3

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.

4
  • When duplicates are found, how do you want to decide which one is to remain, and which one(s) are to be deleted? Commented May 24, 2010 at 6:07
  • @Tomas always the first one to remain... Commented May 24, 2010 at 6:09
  • 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? Commented May 24, 2010 at 6:16
  • @strelokstrelok ya i want the latter one... Commented May 24, 2010 at 6:19

5 Answers 5

4

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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();

Comments

0

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

1 Comment

@Pandiya, well, why didn't you say so? =)
0

You can use "IEqualityComparer" in C#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.