Posted By

billi8324 on 08/16/19


Tagged

tables


Versions (?)

Remove duplicate rows on table


 / Published in: C#
 

Remove duplicate rows on tables c#

  1. public DataTable RemoveDuplicateRows(DataTable table, string DistinctColumn)
  2. {
  3. try
  4. {
  5. ArrayList UniqueRecords = new ArrayList();
  6. ArrayList DuplicateRecords = new ArrayList();
  7.  
  8. // Check if records is already added to UniqueRecords otherwise,
  9. // Add the records to DuplicateRecords
  10. foreach(DataRow dRow in table.Rows)
  11. {
  12. if (UniqueRecords.Contains(dRow[DistinctColumn]))
  13. DuplicateRecords.Add(dRow);
  14. else
  15. UniqueRecords.Add(dRow[DistinctColumn]);
  16. }
  17.  
  18. // Remove duplicate rows from DataTable added to DuplicateRecords
  19. foreach (DataRow dRow in DuplicateRecords)
  20. {
  21. table.Rows.Remove(dRow);
  22. }
  23.  
  24. // Return the clean DataTable which contains unique records.
  25. return table;
  26. }
  27. catch (Exception ex)
  28. {
  29. return null;
  30. }
  31. }

Report this snippet  

You need to login to post a comment.