How to remove rows based on values from a datatable in Linq Query? Please Help

RemoveVal.xaml (10.4 KB) I need to remove rows in a datatable based on list of values in another datatable.

From the Source Datatable, I need to remove entire rows if values in column “Col 2” matches with values found in column “Nos” in Datatbable DT1.
Please help me to solve this.

What I have:
Source Datatable:
image

and DT1 (it contains more than 1000 rows in actual data)
image

and what I need in new FinalDT
image

Since dealing with datatable with large data, I am trying in LinQ Query instead of For Each loop:

Please help me

Hi @GuhanTM,

You can create a list of all the values in the DT1 with the below LINQ
dtList = dt1.AsEnumerable().Select(Function (a) a.Field(Of String)(“Nos”).ToString).ToList()

And then you can use the below LINQ to create a new datatable having only the rows not present in DT1
FinalDT = SourceDT.AsEnumerable.Where(Function(row) Not dtList.Contains(row(“Col 2”).ToString)).CopyToDataTable

Happy coding! :slight_smile:

7 Likes

@Udhay Thanks for the reply.
It is working as expected.
Thanks again.

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