LINQ Get rows from DT1 that are in DT2

Hi!
I have two datatable, for example:
image

And I want with LINQ get rows from DT1 that are in DT2 that is
image

But, if in DT1 there are no rows that are in DT2, then DT1 must be clear!!! Its important.

This LINQ from join work a very long time:
DT1 = (From x In DT1.AsEnumerable() where (From a In DT1.AsEnumerable() Join b In DT2.AsEnumerable() On a(β€œID”).ToString() Equals b(β€œID”).ToString() select a).Contains(x) select x).CopyToDataTable()

In general we can do:

dtResult =

(From d1 in dt1.AsEnumerable
Where dt2.AsEnumerable.Any(Function (d2) d2("ID").toString.Trim.Equals(d1("ID").ToString.Trim))
Select r = d1).CopyToDataTable

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

A general approach was described above

the LINQ statement is creating unneeded processing with introducing the where part and other points

Depending on some other factors also alternates are applicable e.g

  • setoperation intersect / except can be done
  • id list calculations

what is r in this?

r is like a local variable on which d1 is assignes to

In some scenarios this line was more reliable (less compiler issues) as the short version
Select d1).CopyTo…

decide by your own, which variation you ant to use

Unable tO add the function, from methods in my Uipath

Copytodatatable Function will generate automatically when we Type

So,After closed parenthesis write it as .copytodatatable manually

Then it will work fine.

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