【发布时间】:2015-04-24 11:31:26
【问题描述】:
我正在尝试比较两个数据表并仅返回有变化的行。在下面的代码中,我将 dt2 克隆到 dt3 中,但它不会选择有更改的行,而是将所有内容都放在那里。请提出一些选择。
即使修改了单个单元格值,我也需要选择该行。每次比较时,这两个数据表都具有相同的行和列集。
Table 1 Table 2
ID Name ID Name
1 Mark 1 Mark
2 Spencer 2 Spencer
3 Ryan 3 George
表 3 预期结果:
ID Name
3 George
代码:
DataTable dt3 = new DataTable();
// It will compare the 2nd table with the 1st table
var contacts = dt2.AsEnumerable().Except(dt1.AsEnumerable(), DataRowComparer.Default);
dt3 = dt2.Clone();
foreach (DataRow rows in contacts)
{
dt3.ImportRow(rows);
}
【问题讨论】: