Datatable comparision -- between two DTs and get the common records

@ppr @supermanPunch

i have DT1
image

and DT2

image

need to get the matched record from DT2

note:-In DT1 only 2 column and in DT2 3 columns cannot use datarow comparer

1 Like

Hi @Sree_Krishnan_vellinezhi ,

Could you also mention/provide the expected Output for the Inputs provided ?

in general it is a Match case. We can model it with the Any Approach (did not use innerjoin as we dont want to get confuced by duplicated personal nos)

dtResult =

(From d in dt2.AsEnumerable
Let cv = d("Personal no").toString.Trim
Where dt1.AsEnumerable.Any(Function (x) x("Personal no").toString.Trim.Equals(cv))
Select r = d).CopyToDataTable

Handling empty results:
:ambulance: :sos: [FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum

1 Like

You can get it using Linq by comparing a primary column value in DT1 and DT2

(From row2 in dt_2.AsEnumerable from row1 in DT_1.AsEnumerable where row2(“Personal number”).Equals(row1(“Personal number”))select row2).CopyToDataTable.

In place of
‘Personal Number’ as primary column, you can put either column name or column index.

Thanks
Happy Automation

image

expected output

hi @ppr not only first column need to check 2 columns

(From d in dt2.AsEnumerable
Let cv = d("Personal no").toString.Trim
Let dv = d("Date").toString.Trim
Where dt1.AsEnumerable.Any(Function (x) x("Personal no").toString.Trim.Equals(cv) And x("Date").toString.Trim.Equals(dv))
Select r = d).CopyToDataTable

As we do have only 2 cols wie can concat. As an alternate we can work with sequence equals

(From d in dt2.AsEnumerable
Let pa = d.ItemArray.Take(2)
Where dt1.AsEnumerable.Any(Function (x) x.ItemArray.Take(2).SequenceEqual(pa))
Select r = d).CopyToDataTable

Hope this Linq will solve your issue.

(From row2 in dt_2.AsEnumerable from row1 in DT_1.AsEnumerable where(row2(“Personal no”).Equals(row1(“Personal no”)) AndAlso row2(“Date”).Equals(row1(“Date”))) select row2).CopyToDataTable

I hope you want to compare ‘Personal no’ and ‘Date’
column.

Thanks
Happy Automation

not getting

Could you please share the Screenshot of the error you’re getting?

Thanks,

Your solution worked !! thanks…

1 Like

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