The source contains no DataRows and comparing datatables

Hi,

I am using the following LINQ query to get rows that exists in DT1 but not in DT2:

DT1.AsEnumerable().Except(DT2.AsEnumerable, DataRowComparer.Default).CopyToDataTable

The problem is when the datatables are identical(no missmatches) then I get the Error: “Assing: The Source contains no DataRows”.

I tried to wrap it around an IF statement that checked if it was Null but that does not work.

How can I make it so when the LINQ query doesnt find any missmatch to assing a variable the value FALSE.

@atomic,

Can you please follow below link?

I guess you will find a solution there if not, please let us know…

Cheers,
Pankaj

1 Like

we can handle retrun results with following pattern

CopytoDataTable is throwing an exception in case of no rows are returned / forwarded to the method. But with checking on this before using CopytoDataTable we can handle this in a defensive way

1 Like

@ppr im not 100% sure if im on the same page.

Do I need to convert my datatable to a List first?
Because with the Linq Query im using above I am comparing 2 datatables.

I cant see how the Linq Query you wrote compares the two datatables?

I have not merged the datatables.

Is there a way to make it that when DT1.AsEnumerable().Except(DT2.AsEnumerable, DataRowComparer.Default).CopyToDataTable throws an exception then assign it a value.

DT1.AsEnumerable().Except(DT2.AsEnumerable, DataRowComparer.Default).ToList
then check the count as done in the pattern
based on the check you execute the CopyToDataTable
OR
Your clone it to reveive an empty datatable within the same column structure

1 Like

@ppr Thank you. Works great.

So lists are easier to work with it seems.

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