c中两个数据表的差异

本文关键字:数据表 两个 | 更新日期: 2023-09-27 18:24:32

我有两个数据表,如下

dtOne
-------------------------
  ID  |   Name 
--------------------------
 101  |  ABC
 102  |  XYZ
 103  |  MNO
--------------------------
dtTwo
-------------------------
  ID  |   Name 
--------------------------
 101  |  ABC
 102  |  XYZ
--------------------------

我只想把结果作为in dtOnenot in dtTwo(dtOnedtTwo)的数据

dtResult
-------------------------
  ID  |   Name 
--------------------------
 103  |  MNO
--------------------------

我怎样才能做到这一点。

c中两个数据表的差异

为了让它发挥作用,最好使用Linq TO DataSet将很容易解决它。。

DataTable table1= ds.Tables["table1"];
DataTable table2= ds.Tables["table2"];
var diff= table1.AsEnumerable().Except(table2.AsEnumerable(),
                                                    DataRowComparer.Default);

从在DataTable上的LINQ查询下显示的解决方案开始,我会尝试使用:

var dtOneData = from myRow in dtOne.AsEnumerable();
var dtTwoData = from myRow in dtOne.AsEnumerable();
var difference = dtOneData.Except(dtTwoData);