简体   繁体   English

比较两个数据表并获取C#中的差异行

[英]compare two datatables and get difference rows in C#

Here is an example, let say I have two datatables. 这是一个示例,假设我有两个数据表。

Table_1
id     name     age
===================
1      john     20
2      henry    25
3      sam      18
4      tom      30


Table_2
id     name     age
===================
1      john     20
2      henry    26     <=== Edited Row
3      sam      19     <=== Edited Row 
4      tom      30

Those two tables are in diffenrence database ( with same schema ). 这两个表位于差异数据库(具有相同的架构)中。
I loaded them into two DataTable and find difference rows by using Except like 我他们装成两个数据表,找到差异行使用Except

dt_Table_1.AsEnumerable().Except(dt_Table_2.AsEnumerable())

Using Except returns only newly inserted rows but not Edited Rows . 使用Except仅返回新插入的行,而不返回Edited Rows。
I just want to get the Edited Rows. 我只想获取“已编辑的行”。
Above tables are just examples, my real data has many rows, so I have to consider about the performance.That's why I don't want to do with looping for each row. 上面的表格仅是示例,我的实际数据有很多行,因此我必须考虑性能。这就是为什么我不想对每一行进行循环。
Is there any better way to do it ? 有什么更好的办法吗?

you can get the changed rows from datatable and add them to your result. 您可以从数据表中获取更改的行,并将其添加到结果中。

DataTable changedRecordsTable = dataTable1.GetChanges();//get changed rows

dt_Table_1.AsEnumerable().Except(dt_Table_2.AsEnumerable()).Union(changedRecordsTable.AsEnumerable())

you should do this before calling accept changes and keep in mind that this compares the hash for values which is different for two different instances and hence result will have all rows from all datatables(ie duplicates are not removed) 您应该在调用accept changes之前执行此操作,并要记住,这会比较散列中两个不同实例的值不同,因此结果将具有所有datatables中的所有行(即不删除重复项)

You can simple construct a query that will give you the rows that changed: 您可以简单地构造一个查询,该查询将为您提供已更改的行:

select * from table_1 inner join table_2 on table_1.id=table_2.id and table_1.age<>table_2.age

However if you add or delete rows, they will not appear, you can run 但是,如果添加或删除行,它们将不会出现,则可以运行

select * from table_1 where id not in (select id from table_2)

To get all those deleted from table_1 and 要从table_1中删除所有这些内容,并

select * from table_2 where id not in (select id from table_1)

to get the rows deleted from table_2 获取从table_2中删除的行

You can combine them all in a single query and put a status (deleted or edited) by running this: 您可以将它们全部合并到一个查询中,并通过运行以下命令来设置状态(已删除或已修改):

select table_1.id, table_1.name, table_1.age, 'edited' as rowstatus from table_1 inner join table_2 on table_1.id=table_2.id and table_1.age<>table_2.age
union
select id, name, age, 'deleted table_1' as rowstatus from table_1 where id not in (select id from table_2)
union
select id, name, age, 'deleted from table_2' as rowstatus from table_2 where id not in (select id from table_1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM