使用linq从两个数据表中过滤

时间:2015-01-02 07:22:55

标签: c# linq

有两个名为dtSource和dtDest的数据表。从这两个数据表中必须过滤在第一列(PRIMARY)中都具有不匹配值的行。

这就是我所做的:

var valueMismatchInBoth = from c in dtSource.AsEnumerable() 
                          where (from o in dtDest.AsEnumerable() 
                                 select o["PRIMARY"]).Contains(c["PRIMARY"].ToString()) 
                          select c;

1 个答案:

答案 0 :(得分:0)

希望此代码有帮助

var valuematchInBoth = from c in dtSource.AsEnumerable()
                          join o in dtDest.AsEnumerable()
                          on o["PRIMARY"] equals c["PRIMARY"]
                          select c;

var valueMismatchInBoth = dtSource.Except(valuematchInBoth);

另一种解决方案是

var valueMismatchInBoth = from c in dtSource.AsEnumerable()
                          from o in dtDest.AsEnumerable()
                          where o["PRIMARY"] != c["PRIMARY"]
                          select c;