How to compare two Datatable completely(exclude for each)?

Hi guys!
I want to compare two datatable, if dt1 and dt2 have same columns, rows and values, it’s true, otherwise it’s false.
I have search in forum and a lot of topics about this.
But I can’t find out the accurate answers.
I try the dt1.equals(dt2) and dt1=dt2, both of them can’t work.
BestRegards

Hello,

for columns and row count it’s quite simple, just

df1.Rows.Count = df2.Rows.Count

df1.Columns.Contains(df2.Columns) → not sure about this, you can check it out. :slight_smile:

For values you’d need to use LINQ query to get from df1:

(From x In df1).ToList → this would turn it to the list of DataRow

Then the same for df2.

At last you can retrieve each row from each list and compare it, also using LINQ query.

Quite challenging task. :slight_smile: Hope I’ve helped in something.

Best,

Artur

1 Like

Hi Artur
Thanks for your reply quickly.
I means the dt1 and dt2 all the same.
I have seen that some guys said they can do it not use for each or linq.
I think just like “=” equals etc.
It should be a easy way. :sweat_smile:

give a try on checking it with the help of a set operator

{dt1.AsEnumerable.Intersect(dt2.AsEnumerable, DataRowComparer.Default).Count, dt1.Rows.Count, dt2.Rows.Count).All(Function (x) x)

But keep in mind: It will have side effects in case of duplicates within the data tables

Also check out following option, checking all rows, all columns are in same order and having same value:

dt1.AsEnumerable.SelectMany(Function (x) x.ItemArray).SequenceEqual(dt2.AsEnumerable.SelectMany(Function (x) x.ItemArray))

And also:

dt1.AsEnumerable.Select(Function (x) x.ItemArray).SequenceEqual(dt2.AsEnumerable.Select(Function (x) x.ItemArray))
2 Likes

Hi ppr

It’s a solution for my issue.
But do you know why equals method can’t support for compare datatable?
BestRegards
Donghai

Hi guys.
My colleague’s opinion, Output Data Table Activity, It’s OK.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.