比较两个数据表在 C# 中的差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10984453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:02:30  来源:igfitidea点击:

Compare two DataTables for differences in C#?

c#linqdatatable

提问by user1256789

Possible Duplicate:
C#, how to compare two datatables A + B, how to show rows which are in B but not in A

可能的重复:
C#,如何比较两个数据表 A + B,如何显示在 B 中但不在 A 中的行

I need to compare two datatables in c# to find the differences. The two datatables have the same schema. What is the best way to do it? Can it be done using linq queries? If yes, How?

我需要比较 c# 中的两个数据表以找到差异。这两个数据表具有相同的架构。最好的方法是什么?可以使用 linq 查询来完成吗?如果是,如何?

采纳答案by Trisped

You could use LINQ to join the two DataTableobjects, matching every column. Then take the IQueryableand find all the rows in the first two DataTableobjects which are not in the IQueryable.

您可以使用 LINQ 连接两个DataTable对象,匹配每一列。然后取IQueryable并找到前两个DataTable对象中不在 中的所有行IQueryable

Example:

示例

private void SampleSolution(DataTable dt1, DataTable dt2)
{
    //If you have primary keys:
    var results = from table1 in dt1.AsEnumerable()
                    join table2 in dt2.AsEnumerable() on table1.Field<int>("id") equals table2.Field<int>("id")
                    where table1.Field<int>("ColumnA") != table2.Field<int>("ColumnA") || table1.Field<int>("ColumnB") != table2.Field<int>("ColumnB") || table1.Field<String>("ColumnC") != table2.Field<String>("ColumnC")
                    select table1;
    //This will give you the rows in dt1 which do not match the rows in dt2.  You will need to expand the where clause to include all your columns.


    //If you do not have primarry keys then you will need to match up each column and then find the missing.
    var matched = from table1 in dt1.AsEnumerable()
                    join table2 in dt2.AsEnumerable() on table1.Field<int>("ColumnA") equals table2.Field<int>("ColumnA")
                    where table1.Field<int>("ColumnB") == table2.Field<int>("ColumnB") || table1.Field<string>("ColumnC") == table2.Field<string>("ColumnC") || table1.Field<object>("ColumnD") == table2.Field<object>("ColumnD")
                    select table1;
    var missing = from table1 in dt1.AsEnumerable()
                    where !matched.Contains(table1)
                    select table1;
    //This should give you the rows which do not have a match.  You will need to expand the where clause to include all your columns.
}

The code above should work, though I did not test it.

上面的代码应该可以工作,但我没有测试它。

You could also check out LINQ query on a DataTablewhich has some useful information on using LINQ with DataTables.
I also find the LINQ samplesto be helpful when writting LINQ.

您还可以查看DataTable 上的 LINQ 查询,其中包含一些有关将 LINQ 与 DataTables 一起使用的有用信息。
我还发现LINQ 示例在编写 LINQ 时很有帮助。