Keep duplicate rows using hashtable

H

hung tran

Hi,

Here is the code to eliminate duplicate rows, but what if I want to keep
them and elimiante all other the unique ?


public void RemoveDuplicateRows(DataTable dTable,string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();

foreach(DataRow drow in dTable.Rows)
{
try
{
hTable.Add(drow[colName],string.Empty);
}
catch
{
duplicateList.Add(drow);
}
}

foreach(DataRow dRow in duplicateList)
dTable.Rows.Remove(dRow);
}

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Hung,

You will have to make two passes. On the first pass, you will have to
add the rows to the list that is stored with a certain key.

Then, you have to cycle through all the lists. If there is only one
item in the list that is related by a certain key, then you delete it.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top