I have a DataTable like this
name age ------------ kumar 27 kiran 29 anu 24 peter 34 tom 26 manu 35 sachin 37 geetha 23
Now I have another DataTable like this with one column:
name ---- manu tom anu
I need to compare the value in the column name here and remove all the rows that share the same name. Now the result output should be like this:
name age ------------ kumar 27 kiran 29 peter 34 sachin 37 geetha 23
How can I achive this result?
Assuming you mean .NET datatables and not SQL tables. One approach is the following. Convert your second datatable to a dictionary, something like this:
You must loop from end to first, otherwise you get problems (cannot use
foreachhere, it’ll throw an exception).Edit: better solution
Partially inspired by a little discussion with Jerod below, I figured there had to be a way to do this with a LINQ expression that’s easy to read and apply. Previously, it didn’t work, because I enumerated the DataRowView itself, which, when you try to remove something, will raise an
InvalidOperationException, telling you that the collection was modified. Here’s what I did, and it works because we useSelectto create a copy of the row items: