比较不同表的数据行

发布于 2024-12-29 16:18:57 字数 731 浏览 0 评论 0原文

我不久前发布了一个类似查询,并决定将其复杂性降低到让开发人员回答我的主要问题。它可以说是重复的,但我仍然想将其发布,因为编辑上一篇文章并没有产生太多结果。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甩你一脸翔 2025-01-05 16:18:57
var result= dataTable1.AsEnumerable().Intersect(dataTable2.AsEnumerable(),
                                                    DataRowComparer.Default);

它返回两个表中的记录

更多信息:

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

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

决绝 2025-01-05 16:18:57

您可以使用 Equals 方法 DataRowComparer 到比较行。

You could use the Equals method of the DataRowComparer class to compare the rows.

醉梦枕江山 2025-01-05 16:18:57

使用 SequenceEqual 比较两个数据行,如下例所示

foreach (DataRow dr in datatable1.Rows)
    foreach (DataRow dr2 in datatable2.Rows)
    {
        if (dr.ItemArray.SequenceEqual(dr2.ItemArray))
        {
            MessageBox.Show("dr = dr2");
            //statement                                    
        }
        else
        {
            MessageBox.Show("dr != dr2");
            //statement
        }
    }

Using SequenceEqual to compare two data row as in the following example

foreach (DataRow dr in datatable1.Rows)
    foreach (DataRow dr2 in datatable2.Rows)
    {
        if (dr.ItemArray.SequenceEqual(dr2.ItemArray))
        {
            MessageBox.Show("dr = dr2");
            //statement                                    
        }
        else
        {
            MessageBox.Show("dr != dr2");
            //statement
        }
    }
清欢 2025-01-05 16:18:57

为简单起见,我通常会将 ItemArray 中的项目转换为字符串并以这种方式进行比较。
据我所知,使用 GetHashCode 不会产生与许多其他人所说的相同的结果。
如果您有大量行,那么您可以尝试创建一个继承自 DataRow 的类并重写 Equals 方法。例如:
<代码>
类自定义行:数据行
{

    public override bool Equals(object obj)
    {
        if(obj.GetType() != typeof(CustomRow)) return false;
        for (int i = 0; i < ItemArray.Length; i++)
            if (((CustomRow)obj)[i] != this[i])
                return false;

        return true;
    }
}

<代码>

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
{

    public override bool Equals(object obj)
    {
        if(obj.GetType() != typeof(CustomRow)) return false;
        for (int i = 0; i < ItemArray.Length; i++)
            if (((CustomRow)obj)[i] != this[i])
                return false;

        return true;
    }
}

娇纵 2025-01-05 16:18:57

另一种选择是:

DataView dv = new DataView(dataTable1); 
dv.Filter = "SQL query to find specific row"

这自然适用于您会找到的每种生食

Another option is:

DataView dv = new DataView(dataTable1); 
dv.Filter = "SQL query to find specific row"

This, naturally, is for every raw you gonna find

烟酉 2025-01-05 16:18:57

DataRowComparer 自 .NET 3.5 起就可用,但您无法找到如何使用它的示例。现在您可以:

bool tf;
// Unbelievable...
DataRowComparer<DataRow> drc = DataRowComparer.Default;
DataTable tbl;

tbl = new DataTable();
tbl.Columns.Add("One", typeof(string));
tbl.Columns.Add("Two", typeof(string));
tbl.Columns.Add("Three", typeof(string));
tbl.Rows.Add("One", "Two", "Three");
tbl.Rows.Add("One", "Two", "Three");
tbl.Rows.Add("One", "Three", "Two");
tbl.Rows.Add("One", "Two");
tbl.Rows.Add("One", "Two");
tf = dc.Equals(tbl.Rows[0], tbl.Rows[1]);
tf = dc.Equals(tbl.Rows[0], tbl.Rows[2]);
tbl2 = new DataTable();
tbl2.Columns.Add("One", typeof(string));
tbl2.Columns.Add("Two", typeof(string));
tbl2.Columns.Add("Nine", typeof(string));  // Diff col name
tbl2.Rows.Add("One", "Two", "Three");
tbl2.Rows.Add("One", "Two", "Three");
tbl2.Rows.Add("One", "Three", "Two");
tbl2.Rows.Add("One", "Two");
tbl2.Rows.Add("One", "Two");
tf = dc.Equals(tbl.Rows[0], tbl2.Rows[0]);
tf = dc.Equals(tbl.Rows[1], tbl2.Rows[1]);
tf = dc.Equals(tbl.Rows[0], tbl2.Rows[1]);
tf = dc.Equals(tbl.Rows[0], tbl2.Rows[2]);

但我喜欢这个以及使用 DataRowComparer 实例化 - 我需要看到性能优势。

tbl.Rows[0].ItemArray.SequenceEquals(tbl.Rows[1]);

The DataRowComparer has been available since .NET 3.5, not that you can find an example of how to use it. Now you can:

bool tf;
// Unbelievable...
DataRowComparer<DataRow> drc = DataRowComparer.Default;
DataTable tbl;

tbl = new DataTable();
tbl.Columns.Add("One", typeof(string));
tbl.Columns.Add("Two", typeof(string));
tbl.Columns.Add("Three", typeof(string));
tbl.Rows.Add("One", "Two", "Three");
tbl.Rows.Add("One", "Two", "Three");
tbl.Rows.Add("One", "Three", "Two");
tbl.Rows.Add("One", "Two");
tbl.Rows.Add("One", "Two");
tf = dc.Equals(tbl.Rows[0], tbl.Rows[1]);
tf = dc.Equals(tbl.Rows[0], tbl.Rows[2]);
tbl2 = new DataTable();
tbl2.Columns.Add("One", typeof(string));
tbl2.Columns.Add("Two", typeof(string));
tbl2.Columns.Add("Nine", typeof(string));  // Diff col name
tbl2.Rows.Add("One", "Two", "Three");
tbl2.Rows.Add("One", "Two", "Three");
tbl2.Rows.Add("One", "Three", "Two");
tbl2.Rows.Add("One", "Two");
tbl2.Rows.Add("One", "Two");
tf = dc.Equals(tbl.Rows[0], tbl2.Rows[0]);
tf = dc.Equals(tbl.Rows[1], tbl2.Rows[1]);
tf = dc.Equals(tbl.Rows[0], tbl2.Rows[1]);
tf = dc.Equals(tbl.Rows[0], tbl2.Rows[2]);

But I like this just as well as jacking around with the DataRowComparer instantiation - I'd need to see be a performance advantage.

tbl.Rows[0].ItemArray.SequenceEquals(tbl.Rows[1]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文