Removing Duplicate Rows from a DataTable
This is one of the mostly asked question by many developers. I found the same solution to this problem at various places as given below:
Reference: System.Data
public DataTable RemoveDuplicateRows(DataTable table, string DistinctColumn)
{
try
{
ArrayList UniqueRecords = new ArrayList();
ArrayList DuplicateRecords = new ArrayList();
// Check if records is already added to UniqueRecords otherwise,
// Add the records to DuplicateRecords
foreach(DataRow dRow in table.Rows)
{
if (UniqueRecords.Contains(dRow[DistinctColumn]))
DuplicateRecords.Add(dRow);
else
UniqueRecords.Add(dRow[DistinctColumn]);
}
// Remove dupliate rows from DataTable added to DuplicateRecords
foreach (DataRow dRow in DuplicateRecords)
{
table.Rows.Remove(dRow);
}
// Return the clean DataTable which contains unique records.
return table;
}
catch (Exception ex)
{
return null;
}
}
How to use this function:
DataTable DuplicateRecords = objDatabase.getTable("SQL Query");
DataTable UniqueRecords = RemoveDuplicateRows(DuplicateRecords, "Column Name to check for duplicate records");
The problem I found in the code is that the function is working for one distinct column only. What we do if we want to have more than one distinct column names in our query??
Eventually i got a solution to this which is smaller and faster than the above code. I hope this will answer to your query regarding removal of duplicate records in a DataTable:
var uniquerows = MsgsToSend2.AsEnumerable().Distinct(DataRowComparer.Default);
DataTable Contacts = uniquerows.CopyToDataTable();
So the above two lines will serve your purpose. Here MsgsToSend2 is the DataTable containing the duplicate rows and the Contacts DataTable will have the unique rows. This is all about the advantage of LINQ (Language Integrated Query).
Comments