比较不同表的数据行 [英] compare datarows of different tables

查看:304
本文介绍了比较不同表的数据行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前发布了类似查询,并决定缩减复杂性让它让开发者回答我的主要问题。它可以表示为重复,但仍然我想张贴它作为编辑以前的帖子没有产生很多结果。



我有2个数据表:dataTable1和dataTable2。两者都有1行具有相同的条目。例如。这两个数据表中的列为Name,Class,Subject。现在dataTable的两行都与值(John,5,Science)相同。现在我想比较thses 2行,如果他们有相同的条目或不。我尝试:

  if(dataTable1.Rows [0] .GetHashCode()== dataTable2.Rows [0] .GetHashCode ())
{
//结果为false(但我预期它为true)
}

并尝试了:

  if(dataTable1.Rows [0] .ItemArray == databable2.Rows [0] .ItemArray)
{
//结果为false(但我预期它为true)
}
pre>

我想避免循环,但如果需要的话。我只想比较2行2个不同的数据表,如果他们的条目是否相同。我不知道如何继续。感谢。

解决方案

  var result = dataTable1.AsEnumerable()。Intersect(dataTable2.AsEnumerable (),
DataRowComparer.Default);

它返回表中的记录



更多信息:



http://msdn.microsoft.com/en-us/library/bb386998.aspx


I posted a similar query some time ago and decided to trim down the complexity of it to let developers answer my main problem. It could be stated as duplicate, but still I want to post it as editing the previous post did not yield much result.

I have 2 datatables: dataTable1 and dataTable2. Both have 1 row with same entries. For eg. columns in both the datatables are Name, Class, Subject. Now both row of both the dataTable are same with values ("John", "5", "Science"). Now I want to compare thses 2 rows if they they have same entries or not. I tried for:

if(dataTable1.Rows[0].GetHashCode() == dataTable2.Rows[0].GetHashCode()) 
{ 
    // Result is false (but I expected it to be true) 
} 

And also tried:

if(dataTable1.Rows[0].ItemArray == dataTable2.Rows[0].ItemArray) 
{ 
    // Result is false (but I expected it to be true) 
} 

I want to avoid loops to do it, but if needed thats fine. I just want to compare the 2 rows of 2 different dataTables that if their entries are same or not. And I am not sure how to proceed. Thanks.

解决方案

var result= dataTable1.AsEnumerable().Intersect(dataTable2.AsEnumerable(),
                                                    DataRowComparer.Default);

it returns the records that are in both the table

more info at:

http://msdn.microsoft.com/en-us/library/bb386998.aspx

这篇关于比较不同表的数据行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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