本文介绍了如何使用Linq或任何其他表达式从DataTable1获取除DataTable2之外的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从DataTable1获取除DataTable2之外的所有行.两者都有共同的ID(integere)列?
How to get all rows from DataTable1 Except DataTable2. Both have a common ID(integere) column?
推荐答案
DataRow drT = null;
DataTable dt1 = new DataTable();
dt1.Columns.Add("Column1");
drT = dt1.NewRow();
drT["Column1"] = "value 1";
dt1.Rows.Add(drT);
drT = dt1.NewRow();
drT["Column1"] = "value 2";
dt1.Rows.Add(drT);
drT = dt1.NewRow();
drT["Column1"] = "value 3";
dt1.Rows.Add(drT);
drT = dt1.NewRow();
drT["Column1"] = "value 4";
dt1.Rows.Add(drT);
DataTable dt2 = new DataTable();
dt2.Columns.Add("Column1");
drT = dt2.NewRow();
drT["Column1"] = "value 1";
dt2.Rows.Add(drT);
drT = dt2.NewRow();
drT["Column1"] = "value 2";
dt2.Rows.Add(drT);
drT = dt2.NewRow();
drT["Column1"] = "value 5";
dt2.Rows.Add(drT);
drT = dt2.NewRow();
drT["Column1"] = "value 6";
dt2.Rows.Add(drT);
IEnumerable<DataRow> ieDrDt1 = dt1.AsEnumerable();
IEnumerable<DataRow> ieDrDt2 = dt2.AsEnumerable();
var exceptData = ieDrDt1.Except(ieDrDt2, DataRowComparer.Default);
foreach (DataRow dr in exceptData)
{
Response.Write(dr["Column1"].ToString() + "<br />");
}
这篇关于如何使用Linq或任何其他表达式从DataTable1获取除DataTable2之外的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!