How to filter a table based on another table with LINQ

Let’s say I have table1:

image

And table2:

image

And I need to filter column ID from table1 based on column ID from table2, the expected result would be:

image

I tried this but it never worked:
table1.AsEnumerable.Where(x => table2.AsEnumerable.Any(y => y(“ID”).ToString == x(“ID”).ToString)).CopyToDataTable

1 Like

@diego.montero.gonzalez

we can use within an assign the query syntax:

(From d In table1.AsEnumerable 
Where table2.AsEnumerable.Any(Function (d2) d2("ID").ToString.Equals(d("ID").ToString))).CopyToDataTable

You just used the LINQ syntax from C# which is different

Within the method syntax it would look like:

  table1.AsEnumerable.Where(Function (d) table2.AsEnumerable.Any(Function (d2) d2("ID").ToString.Equals(d("ID").ToString))).CopyToDataTable
3 Likes

I was actually looking a similar answer made by you. It worked :slight_smile: thank you so much!

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