Datatable Comparison Linq

Hi There,

I have used LINQ to remove data/rows that exists in another datatable as below
(From d In Dt1.AsEnumerable()
Where Not Dt2.AsEnumerable.Any(Function (x) x(“ID”).toString.Equals(d(“ID”).ToString))
Select d).CopyToDataTable

The problem is sometimes all data in Dt1 exists in Dt2 so output is 0 that throws error. Please assist how to handle that exception

@ibraalli

Have a if condition and first check if there is any data that is getting retreived as below.

(From d In Dt1.AsEnumerable() Where Not Dt2.AsEnumerable.Any(Function (x) x(“ID”).toString.Equals(d(“ID”).ToString)) Select d).Count>0

On then side use your copydatatable and on else side use dt1.Clone()

So that when there are no rows then datatable is still initialized for next steps with zero rows and we are already checking for zero rows

Hope this helps

Cheers

Hey

you can put this within a try catch acticity, so if this go to the catch field then you can perform an initialization there

Regards

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

Hi @ibraalli,

You can use a try-catch block to handle the exception. Try the following code:

Try
(From d In Dt1.AsEnumerable()
Where Not Dt2.AsEnumerable.Any(Function (x) x(“ID”).toString.Equals(d(“ID”).ToString))
Select d).CopyToDataTable
Catch ex As Exception
'Do something with the exception here
End Try

Thanks,

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