I know this is, very, old question, and I have similar situation few days ago.
Problem was, in my table are approx. 10000 rows, so looping trough DataTable
rows was very slow.
Finally, I found much faster solution, where I make copy of source DataTable
with desired results, clear source DataTable
and merge
results from temporary DataTable
into source one.
note : instead search for Joe
in DataRow
called name
You have to search for all records whose not have name Joe
(little opposite way of searching)
There is example (vb.net
) :
'Copy all rows into tmpTable whose not contain Joe in name DataRow
Dim tmpTable As DataTable = drPerson.Select("name<>'Joe'").CopyToTable
'Clear source DataTable, in Your case dtPerson
dtPerson.Clear()
'merge tmpTable into dtPerson (rows whose name not contain Joe)
dtPerson.Merge(tmpTable)
tmpTable = Nothing
I hope so this shorter solution will help someone.
There is c#
code (not sure is it correct because I used online converter :( ):
//Copy all rows into tmpTable whose not contain Joe in name DataRow
DataTable tmpTable = drPerson.Select("name<>'Joe'").CopyToTable;
//Clear source DataTable, in Your case dtPerson
dtPerson.Clear();
//merge tmpTable into dtPerson (rows whose name not contain Joe)
dtPerson.Merge(tmpTable);
tmpTable = null;
Of course, I used Try/Catch
in case if there is no result (for example, if Your dtPerson
don't contain name
Joe
it will throw exception), so You do nothing with Your table, it stays unchanged.