Remove duplicate column values from a datatable without using LINQ

12,275

Solution 1

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;
    }

Solution 2

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.

Solution 3

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();
Share:
12,275
ACP
Author by

ACP

Updated on June 13, 2022

Comments

  • ACP
    ACP almost 2 years

    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.

    • Tomas Aschan
      Tomas Aschan almost 14 years
      When duplicates are found, how do you want to decide which one is to remain, and which one(s) are to be deleted?
    • ACP
      ACP almost 14 years
      @Tomas always the first one to remain...
    • Strelok
      Strelok almost 14 years
      Do you want to just get a set of records with unique mobile numbers or to remove the records with duplicates from the existing set?
    • ACP
      ACP almost 14 years
      @strelokstrelok ya i want the latter one...
  • Tomas Aschan
    Tomas Aschan almost 14 years
    @Pandiya, well, why didn't you say so? =)