Question

Consider my datatable,

Id  Name  MobNo
1   ac    9566643707
2   bc    9944556612
3   cc    9566643707

How to remove the row 3 which contains duplicate MobNo column value in c# without using LINQ. I have seen similar questions on SO but all the answers uses LINQ.

Was it helpful?

Solution

The following method did what i want....

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

        //Add list of all the unique item value to hashtable, which stores combination of key, value pair.
        //And add duplicate item value in arraylist.
        foreach (DataRow drow in dTable.Rows)
        {
            if (hTable.Contains(drow[colName]))
                duplicateList.Add(drow);
            else
                hTable.Add(drow[colName], string.Empty);
        }

        //Removing a list of duplicate items from datatable.
        foreach (DataRow dRow in duplicateList)
            dTable.Rows.Remove(dRow);

        //Datatable which contains unique records will be return as output.
        return dTable;
    }

OTHER TIPS

As you are reading your CSV file ( a bit of pseudo code, but you get the picture ):

List<String> uniqueMobiles = new List<String>();

String[] fileLines = readYourFile();

for (String line in fileLines) {
   DataRow row = parseLine(line);
   if (uniqueMobiles.Contains(row["MobNum"])
   {
       continue;
   }
   uniqueMobiles.Add(row["MobNum"]);
   yourDataTable.Rows.Add(row);       
}

This will only load the records with unique mobiles into your data table.

You might want to look up the inner workings on DISTINCT before running this on your sharp DB (be sure to back up!), but if it works as I think it does (grabbing the first value) you should be able to use (something very similar to) the following SQL:

DELETE FROM YourTable WHERE Id NOT IN (SELECT DISTINCT Id, MobNo FROM YourTable);

You can use "IEqualityComparer" in C#

This is the simplest way .

**

var uniqueContacts = dt.AsEnumerable()
                       .GroupBy(x=>x.Field<string>("Email"))
                       .Select(g=>g.First());

** I found it in this thread LINQ to remove duplicate rows from a datatable based on the value of a specific row

what actually was for me that I return it as datatable

DataTable uniqueContacts = dt.AsEnumerable()
                           .GroupBy(x=>x.Field<string>("Email"))
                           .Select(g=>g.First()).CopyToDataTable();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top