[转]Linq查询DataTable,DataRow

原文链接:http://www.cnblogs.com/greatwang/archive/2013/01/20/3292539.html

 原文:http://www.cnblogs.com/wucg/archive/2010/10/13/1850053.html


static void Main()         {             string strcon = global::TestLinqToSQL.Properties.Settings.Default.testConnectionString;             SqlConnection conn = new SqlConnection(strcon);             SqlDataAdapter da = new SqlDataAdapter("select * from T_Teacher", strcon);             DataTable dt = new DataTable();             da.Fill(dt);             conn.Close();               IEnumerable<DataRow> q1 = from r in dt.AsEnumerable()                                       where r.Field<int>("ID") % 2 == 0                                       select r;             foreach (var item in q1)             {                 Console.WriteLine("ID={0},Name={1},Birthday={2}", item["ID"].ToString(), item["Name"].ToString(), item["Birthday"].ToString());             }             Console.WriteLine("-----------------------------------");             IEnumerable<DataRow> q2 = from r in dt.AsEnumerable()                                       where r.Field<int>("ID")>10                                       select r;             foreach (var item in q2)             {                 Console.WriteLine("ID={0},Name={1},Birthday={2}", item["ID"].ToString(), item["Name"].ToString(), item["Birthday"].ToString());             }             Console.WriteLine("-----------------------------------");             DataTable dt1 = q1.CopyToDataTable();             DataTable dt2 = q2.CopyToDataTable();      //集合排除
            //var except = dt2.AsEnumerable().Except(dt1.AsEnumerable() , DataRowComparer.Default);
//集合并
            //var except = dt2.AsEnumerable().Union(dt1.AsEnumerable(), DataRowComparer.Default);  //集合交
            var except = dt2.AsEnumerable().Intersect(dt1.AsEnumerable(), DataRowComparer.Default);             foreach (var item in except)             {                 Console.WriteLine("ID={0},Name={1},Birthday={2}", item["ID"].ToString(), item["Name"].ToString(), item["Birthday"].ToString());             }             Console.WriteLine("done");             Console.ReadLine();         }

 


转载于:https://www.cnblogs.com/greatwang/archive/2013/01/20/3292539.html

上一篇:C# 该行已经属于另一个表 的解决方法


下一篇:#私藏项目实操分享# 【Taro工作记录三】按钮实现一键复制文字操作