Perform a LINQ Query with Entity Types - CSharp Database

CSharp examples for Database:LINQ Query

Description

Perform a LINQ Query with Entity Types

Demo Code


using System;/*from w  w w  . j av  a  2 s.c  om*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Data.Linq.Mapping;

    [Table(Name = "Region")]
    public partial class Region
    {
        [Column]
        public int RegionID;
        [Column]
        public string RegionDescription;
    }
class MainClass
    {
        static void Main(string[] args)
        {
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = @"Data Source = .\sqlexpress;" +
                    "Database = Northwind; Integrated Security=SSPI";

                con.Open();

                DataContext context = new DataContext(con);

                Table<Region> regionstable = context.GetTable<Region>();

                IEnumerable<Region> result = from e in regionstable
                                             where e.RegionID < 3
                                             select e;
                foreach (Region res in result)
                {
                    Console.WriteLine("RegionID {0} Descr: {1}", res.RegionID, res.RegionDescription);
                }
            }
        }
    }

Result


Related Tutorials