比较不同表的数据行
我不久前发布了一个类似查询,并决定将其复杂性降低到让开发人员回答我的主要问题。它可以说是重复的,但我仍然想将其发布,因为编辑上一篇文章并没有产生太多结果。
我有 2 个数据表:dataTable1 和 dataTable2。两者都有 1 行包含相同的条目。例如。两个数据表中的列都是名称、班级、主题。现在,两个数据表的两行都具有相同的值(“John”、“5”、“Science”)。现在我想比较这两行是否有相同的条目。我尝试过:
if(dataTable1.Rows[0].GetHashCode() == dataTable2.Rows[0].GetHashCode())
{
// Result is false (but I expected it to be true)
}
并且还尝试过:
if(dataTable1.Rows[0].ItemArray == dataTable2.Rows[0].ItemArray)
{
// Result is false (but I expected it to be true)
}
我想避免循环来做到这一点,但如果需要的话那很好。我只想比较 2 个不同数据表的 2 行,看看它们的条目是否相同。我不知道如何继续。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它返回两个表中的记录
更多信息:
http://msdn .microsoft.com/en-us/library/bb386998.aspx
it returns the records that are in both the table
more info at:
http://msdn.microsoft.com/en-us/library/bb386998.aspx
您可以使用
Equals
方法DataRowComparer
类 到比较行。You could use the
Equals
method of theDataRowComparer
class to compare the rows.使用 SequenceEqual 比较两个数据行,如下例所示
Using SequenceEqual to compare two data row as in the following example
为简单起见,我通常会将
ItemArray
中的项目转换为字符串并以这种方式进行比较。据我所知,使用 GetHashCode 不会产生与许多其他人所说的相同的结果。
如果您有大量行,那么您可以尝试创建一个继承自
DataRow
的类并重写 Equals 方法。例如:<代码>
类自定义行:数据行
{
<代码>
For simplicity, I would normally just cast the item in the
ItemArray
to a string and compare them that way.From what I remember, using the GetHashCode will not bring out the same result as you will find many others will say.
If you have a large number of rows, then you could try creating a class that inherits from
DataRow
and override the Equals method. For example:class CustomRow : DataRow
{
另一种选择是:
这自然适用于您会找到的每种生食
Another option is:
This, naturally, is for every raw you gonna find
DataRowComparer 自 .NET 3.5 起就可用,但您无法找到如何使用它的示例。现在您可以:
但我喜欢这个以及使用 DataRowComparer 实例化 - 我需要看到性能优势。
The DataRowComparer has been available since .NET 3.5, not that you can find an example of how to use it. Now you can:
But I like this just as well as jacking around with the DataRowComparer instantiation - I'd need to see be a performance advantage.