Introduction
In the majority of scenarios duplicated / Non duplicated rows can be identified by a few columns grace to the key column concept. In order to retrieve it, the group by approach helps
It can be done e.g. with some nice components from Connect or LINQ
In some scenarios all duplicated / non duplicated rows should be detected by checking all columns.
Setting up an approach without explicit listing all column names or column index would speed up the implementation.
Implementation
For this task the SequenceEqual method can be used
Lets assume following datatable (only having 2 columns, just to make it small and faster to read)
Finding the Duplicated Rows
With this Linq we retrieve the rows:
(From d In dtData.AsEnumerable()
Let cnt = dtData.AsEnumerable.Where(Function (r) d.ItemArray.SequenceEqual(r.ItemArray)).Count
Where cnt > 1
Select d).toList
- It iterates over all rows
- the current row can be referenced with d
- for each d it is checked for all rows in dtData (referenced with r) if d.ItemArray SequenceEquals r.ItemArray and the true results will be counted, referenced by cnt
- if the count is > 1 duplicates are found for this d and the d is passing the filter
- all found d are added to a list of Datarows that will be returned by this LINQ
Finding the non Duplicats Rows
(From d In dtData.AsEnumerable()
Let cnt = dtData.AsEnumerable.Where(Function (r) d.ItemArray.SequenceEqual(r.ItemArray)).Count
Where cnt = 1
Select d).toList
Same approach but the count has to be excatly 1
With a check on the ListCounts it can be decided if rows are returned or not and can be copied to the corresponding datatable
Result
Finaly we got the resulting DataTables for Duplicates:
the resulting DataTables for Non Duplicates:
Demo XAML Download
FindDupsNonDupsByAllCols.xaml (9.8 KB)
References
Questions
For questions on your retrieval case open a new topic and get individual support