DataTable searchs
-
Hi, how i search for repeated items in a DataTable? Example: I have the Column Name and Email. I want to delete every Name and Email repeateds. Thanks.
For better programming practices use a
UniqueConstraint
on yourDataTable
. To learn more about constraints and data integrity see this msdn article http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaddingconstraintstodataset.asp[^] HTHR.Bischoff
-
Hi, how i search for repeated items in a DataTable? Example: I have the Column Name and Email. I want to delete every Name and Email repeateds. Thanks.
See the
DataTable.Select
command. It uses the same expressions asDataColumn.Expression
. You could use this to get aDataRow[]
array and then remove each of those array members from theDataTable
. Another way would be to sort theDataTable
and iterate - not enumerate, which is whatforeach
does - through it keeping track of the lastDataRow
and comparing it with the currentDataRow
. If they contain the same data, remove the currentDataRow
(which is why you don't enumerate - changing a collection that you're enumerating throws an exception). Don't forget to decrement your index for thefor
loop since it will be incremented with the next iteration (you want to keep the index the same since you just removed aDataRow
).Microsoft MVP, Visual C# My Articles