When comparing DT1 and DT2, retrieve the matching rows Help


two DataTables, DT1 and DT2. In DT1, there are multiple rows, and I want to retrieve the entire rows from DT1 where the data in the “NUMBER” column matches the data in the “NUMBER” column of DT2.

I attempted to use the following code:
DT.AsEnumerable().Where(Function(r2) input.AsEnumerable().Any(Function(r1) DataRowComparer.Default.Equals(r1, r2))).CopyToDataTable()

However, I encountered the error “Read DT all Assign: No DataRow in the source.”
I would appreciate any help with the code!

Thank you in advance.

Hi @22222222asas

Try this query:

resultDataTable = (From row1 In DT1.AsEnumerable()
                                    Join row2 In DT2.AsEnumerable()
                                    On row1("NUMBER").ToString() Equals row2("NUMBER").ToString()
                                    Select row1).CopyToDataTable()

resultDataTable is of DataType System.Data.DataTable
If possible share the DT1 and DT2 data tables with dummy data.


1 Like


You can try like this…first to avaoid the no rows situation before copytodatatable use .Count>0 in if condition and on then side use the .CopyToDatatable and on else side that means there are no matches found

The formula would be as follows in if condition

Dt1.AsEnumerable.Where(function(x) dt2.AsEnumerable.Any(function(y) y("Number").ToString.Equals(x("Number").ToString))).Count>0

On then side

Dt1.AsEnumerable.Where(function(x) dt2.AsEnumerable.Any(function(y) y("Number").ToString.Equals(x("Number").ToString))).CopyToDataTable


1 Like

I’m glad the issue was resolved.
Happy New Year!!

1 Like

I appreciate your response and will use it as a reference.
Happy New Year!!

1 Like

Hi @22222222asas

Thank You. Happy New Year.

Happy Automation !!

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