Linq query for removing matching data and joining

I tried this Linq query but it’s showing error. Pls help me by correcting it

Dt1.Asenumerable.where( Function(x) Dt2.Asenumerable.where(function(y) not y.item(0).tostring.equals(x.item(0).tostring)
).copytotable

I have 2 data tables and need to remove matching data and join the data tables. Pls help

Thanks
Taruna

@taruna.mehra

Please try this

Dt1.AsEnumerable().Except(Dt2.AsEnumerable(), DataRowComparer.Default).Union(Dt2.AsEnumerable().Except(Dt1.AsEnumerable(), DataRowComparer.Default)).CopyToDataTable

This compares all column values…let me know if you want to compare specific columns only

Cheers

Thanks I need to compare specific columns

@taruna.mehra

Try this

(From row1 In dt1.AsEnumerable() Group Join row2 In dt2.AsEnumerable() On row1("ID").ToString = row2("ID").ToString Into temp = Group From row2 In temp.DefaultIfEmpty() Where row1 Is Nothing OrElse row2 Is Nothing Select If(row1 Is Nothing, row2, row1)) .CopyToDataTable()

Cheers

(from d in Dt1.Asenumerable
Let v = d(0).tostring.trim
Let chk = Not dt2.AsEnumerable.Any( function(x) x(0).tostring.trim.eqals(v))
Where chk
Select r = d).copytodatatable

1 Like

Handling empty result