Perform a LINQ Query - CSharp Database

CSharp examples for Database:LINQ Query

Description

Perform a LINQ Query

Demo Code


using System;/*from w  w w.  j  a v a2  s .  com*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

class MainClass
    {
        static void Main(string[] args)
        {

            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = @"Data Source = .\sqlexpress;" + "Database = Northwind; Integrated Security=SSPI";
                con.Open();
                string query = "SELECT * from Region";
                DataSet dataset = new DataSet();
                SqlDataAdapter adapter = new SqlDataAdapter(query, con);
                SqlCommandBuilder commbuilder = new SqlCommandBuilder(adapter);
                adapter.Fill(dataset);
                DataTable table = dataset.Tables[0];
                IEnumerable<string> result = from e in table.AsEnumerable()
                                             where e.Field<int>(0) < 3
                                             select e.Field<string>(1);

                foreach (string str in result)
                {
                    Console.WriteLine("Result: {0}", str);
                }
            }
        }
    }

Result


Related Tutorials