我试图找出基于给定列名的两个数据表之间的差异。我看不出我做错了什么,但是当我传递两个不同的数据表时,我没有得到任何结果。
dt1 dt2
ID Percent ID Percent
--- ------- --- -------
1 100 1 100
2 100 2 100
.. ... .. ...
50 0 50 20
.. ... .. ...
因此,我调用GetTableDiff(dt1,dt2,“Percent”),没有得到一行(ID:50,Percent:20),而是什么也没有得到。
public static DataTable GetTableDiff(DataTable dt1, DataTable dt2, string sColumn)
{
DataTable dtDiff = new DataTable();
try
{
var diff = from r1 in dt1.AsEnumerable()
join r2 in dt2.AsEnumerable()
on r1.Field<int>(sColumn)
equals r2.Field<int>(sColumn) into g
where !g.Any() // get only rows which do not have joined rows from dt2
select r1;
if (diff.Count() > 0)
dtDiff = diff.CopyToDataTable();
}
catch (Exception ex)
{
}
return dtDiff;
}