Linq query to compare two datatable and return values

Hi all,

I have a 2 datatable ,I need to compare the values of two datatable and give the final output,for example:

Dt1

Dt2

In the above tables
I need to compare description of dt1 with dt2.and if matches any then return dt2 columns of
Issue id, description,issue assigned to.

Can any one help me to achieve this using linq query.

Hi @yashashwini2322

Check out this LINQ expression

For Common Values
dt = dt1.AsEnumerable().Intersect(dt2.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable

For Uncommon Values
dt = dt1.AsEnumerable().Except(dt2.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable

Regards
Gokul

Hi @yashashwini2322

Try this

(From row1 In dt1.AsEnumerable()
 Join row2 In dt2.AsEnumerable() On row1("description").ToString() Equals row2("description").ToString()
 Select dt2.Clone().LoadDataRow({row2("Issue id"), row2("description"), row2("issue assigned to")}, False)).CopyToDataTable()

Hii @yashashwini2322

Try this Linq Query

(
From a In DT1
Join b In DT2
On a(“Discription”).ToString Equals b(“Issue_ID”).ToString
Select Out_DT.Rows.Add({a(“Discription”),a(“Date”),b(“Issue_ID”),b(“Issue Assigned To”)})
).CopyToDataTable

1 Like

@yashashwini2322

DT2.AsEnumerable.Where(function(x,i) x(“Description”).ToString.Equals(DT1(“Description”)(i))).CopyToDataTable

I want all the columns of dt 2,without mentioning column name,because column name some time changes and it’s have lot of columns.here I have shown only example.