Sandy
Sandy

Reputation: 11687

compare datarows of different tables

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.

Upvotes: 3

Views: 19986

Answers (6)

B H
B H

Reputation: 1878

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]);

Upvotes: 0

TM7
TM7

Reputation: 51

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
        }
    }

Upvotes: 4

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

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

Upvotes: 6

Dave Watts
Dave Watts

Reputation: 890

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

Upvotes: 4

Tigran
Tigran

Reputation: 62246

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

Upvotes: 0

mrstebo
mrstebo

Reputation: 931

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;
    }
}

Upvotes: 1

Related Questions