Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I want to remove duplicate records from datatable contianing string records, can anybody will help me, i tried the below code but it didnt work.

public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
        {
            Hashtable hTable = new Hashtable();
            ArrayList duplicateList = new ArrayList();
            foreach (DataRow dtRow in dTable.Rows)
            {
                if (hTable.Contains(dtRow[colName].ToString().ToUpper()))
                    duplicateList.Add(dtRow);
                else
                    hTable.Add(dtRow[colName].ToString().ToUpper(), string.Empty);
            }
            foreach (DataRow dtRow in duplicateList)
                dTable.Rows.Remove(dtRow);
            return dTable;
        }



Thanks
Anand
Posted
Comments
MT_ 31-Oct-12 7:14am    
Anand, you mean you want to delete whole record based on one column? Just curious, those are actually two records with just one column value common!
Member 4531085 31-Oct-12 7:46am    
NO i want to remove duplicate records, means ( like we have 3 same records and then i want 1 records ) , but my problem is i have single column with string values.

1 solution

Based on your answer to my comment, You have a table with single column and you want to de-duplicate that table, is that the correct understanding?

If yes, check the following code taken from source below. That should do the trick

SQL
DELETE MyTable
FROM MyTable
LEFT OUTER JOIN (
   SELECT MIN(RowId) as RowId, colName
   FROM MyTable
   GROUP BY colName
) as KeepRows ON
   MyTable.RowId = KeepRows.RowId
WHERE
   KeepRows.RowId IS NULL


Code from : http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows[^]

Hope that helps.
Milind
 
Share this answer
 
Comments
Member 4531085 31-Oct-12 8:08am    
No i didnt get my question, i have datatable having 4 column, out of 4 column i required all the unquie record from 1 column with name "Note". i dont want to use the 3 extra column, but the column i want having duplicate records.
MT_ 31-Oct-12 8:14am    
Could you paste in sample rows ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900