比较两个数据表在C#中的区别? [英] Compare two DataTables for differences in C#?

查看:261
本文介绍了比较两个数据表在C#中的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C#,如何比较两个数据集A + B,如何显示B中的行,但不是在A中


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

解决方案

您可以使用LINQ加入两个 DataTable 对象,匹配每一列。然后采取 IQueryable ,并找到前两个 DataTable 中不在 IQueryable



示例

  private void SampleSolution(DataTable dt1,DataTable dt2)
{
//如果您有主键:
var results = from dt1中的table1.AsEnumerable )
在table1.Field< int>(id)上的dt2.AsEnumerable()中连接table2 equals table2.Field< int>(id)
其中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;
//这将给出dt1中与dt2中的行不匹配的行。您将需要扩展where子句以包含所有列。


//如果没有primarry密钥,那么您将需要匹配每一列,然后找到缺少的。
var matched =来自dt1中的table1.AsEnumerable()
table1.Field< int>(ColumnA)中的dt2.AsEnumerable()中的连接table2等于table2.Field< int>(ColumnA )
其中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 =从dt1中的table1 .AsEnumerable()
其中!matched.Contains(table1)
select table1;
//这应该给你没有匹配的行。您将需要扩展where子句以包含所有列。
}

上面的代码应该可以工作,虽然我没有测试。 >

您还可以查看 DataTable上的LINQ查询其中有一些关于使用LINQ与DataTables的有用信息。

我还发现
LINQ样本在写LINQ时有帮助。


Possible Duplicate:
C#, how to compare two datatables A + B, how to show rows which are in B but not in 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?

解决方案

You could use LINQ to join the two DataTable objects, matching every column. Then take the IQueryable and find all the rows in the first two DataTable objects which are not in the 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 DataTable which has some useful information on using LINQ with DataTables.
I also find the LINQ samples to be helpful when writting LINQ.

这篇关于比较两个数据表在C#中的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆