Compare LINQ DataSet Results - CSharp Database

CSharp examples for Database:LINQ Query

Description

Compare LINQ DataSet Results

Demo Code


using System;//from   w w w  .  ja v a2 s  .co m
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<DataRow> result1 = from e in table.AsEnumerable()
                                              where e.Field<int>(0) < 3
                                              select e;

                Console.WriteLine("Results from first LINQ query");
                foreach (DataRow row in result1)
                {
                    Console.WriteLine("ID: {0} Name: {1}",
                        row.Field<int>(0), row.Field<string>(1));
                }

                IEnumerable<DataRow> result2 = from e in table.AsEnumerable()
                                              let name = e.Field<string>(1)
                                              where name.StartsWith("North")
                                                || name.StartsWith("East")
                                              select e;

                Console.WriteLine("\nResults from second LINQ query");
                foreach (DataRow row in result2)
                {
                    Console.WriteLine("ID: {0} Name: {1}",
                        row.Field<int>(0), row.Field<string>(1));
                }

                IEnumerable<DataRow> union = result1.Union(result2);
                Console.WriteLine("\nResults from union");
                foreach (DataRow row in union)
                {
                    Console.WriteLine("ID: {0} Name: {1}",
                        row.Field<int>(0), row.Field<string>(1));
                }
                IEnumerable<DataRow> intersect = result1.Intersect(result2);
                Console.WriteLine("\nResults from intersect");
                foreach (DataRow row in intersect)
                {
                    Console.WriteLine("ID: {0} Name: {1}",
                        row.Field<int>(0), row.Field<string>(1));
                }

                IEnumerable<DataRow> except = result1.Except(result2);
                // enumerate the results
                Console.WriteLine("\nResults from except");
                foreach (DataRow row in except)
                {
                    Console.WriteLine("ID: {0} Name: {1}",
                        row.Field<int>(0), row.Field<string>(1));
                }

            }
        }
    }

Result


Related Tutorials