I have two data tables, from which i need to compare 3 columns .
one column should match exactly and other columns should match partially(contains).
To check one column we can convert one column of second data table into list and we can loop through but to compare three columns what should be done?
can anyone please suggest the query.
Thanks in advance
image attached for reference…
Name should match partially (contains) AND empID should be exact match(equals)
when we do join for single column we can write like this ,
((From p In DT1.AsEnumerable () Join q In DT2.AsEnumerable () on p.Item(“Column1”).ToString.Trim Equals (q.Item(“Column1”).ToString.Trim) Select p).ToList).CopyToDataTable
((From p In DT1.AsEnumerable () Join q In DT2.AsEnumerable () on p.Item("Column1").ToString.Contains(q.Item("Column1").ToString) Select p).ToList).CopyToDataTable
sorry with join we cant use contains, this will have to be changed to use more complex linq statements that i am afraid im not sure if possible, in one query… maybe @ppr can help you.
As already noticed a Join Statement has to follow some constraints e.g the mandatory use of equals, the Must use of Leftside Datatable equals rightside Datatable referencing etc
However to rewrite IT in a cartesian product can have Chance to get IT done, but also have the risk of Side effects.
Such a Statement would Look Like this
(From p In DT1.AsEnumerable()
From q in DT2.AsEnumerable()
Where p.Item(“Column1”).ToString.Trim.Equals (q.Item(“Column1”).ToString.Trim) and p(colx).to string.contains(q(colx).toString) and …
Select p).ToList
I suggest to Test IT ob sideeffects and correctness. A CopyToDataTable will throw an Error If No rows are returned. So a Check Like resultrowsvar.count>0 is also recommended.