Sunday, August 29, 2010

Remove the Duplicate Rows in DataTable

Removing the Duplicate Rows in DataTable in C# is simple.

Best way is use DISTINCT Keyword in Database Query, but some times we may fill the data manually.

We can remove by using two type

First One is very simple and it is used when we need to compare all columns in the rows.
DataTable _NewTable=_Table.DefaultView.ToTable(true);
Second, Suppose we need to remove the rows for specific columns only means then we can go for the following Method
List<string> keyColumns = new List<string>();
keyColumns.Add("Column1");
keyColumns.Add("Column2");
RemoveDuplicatesFromDataTable(ref _Table, keyColumns);

public void RemoveDuplicatesFromDataTable(ref DataTable table, List<string> keyColumns)

{
Dictionary<string, string> uniquenessDict = new Dictionary<string, string>(table.Rows.Count);
StringBuilder stringBuilder = null;
int rowIndex = 0;
DataRow row;
DataRowCollection rows = table.Rows;
while (rowIndex < rows.Count - 1)
{
row = rows[rowIndex];
stringBuilder = new StringBuilder();
foreach (string colname in keyColumns)
{
stringBuilder.Append(((string)row[colname]));
}
if (uniquenessDict.ContainsKey(stringBuilder.ToString()))
{
rows.Remove(row);
}
else
{
uniquenessDict.Add(stringBuilder.ToString(), string.Empty);
rowIndex++;
}
}
}



No comments:

Post a Comment