Skip to main content

Removing Duplicate Rows from a DataTable in C# (C Sharp)

Removing Duplicate Rows from a DataTable


This is one of the mostly asked question by many developers. I found the same solution to this problem at various places as given below:



Reference: System.Data

public DataTable RemoveDuplicateRows(DataTable table, string DistinctColumn)
{
    try
    {
        ArrayList UniqueRecords = new ArrayList();
        ArrayList DuplicateRecords = new ArrayList();

        // Check if records is already added to UniqueRecords otherwise,
        // Add the records to DuplicateRecords
        foreach(DataRow dRow in table.Rows)
        {
            if (UniqueRecords.Contains(dRow[DistinctColumn]))
                DuplicateRecords.Add(dRow);
            else
                UniqueRecords.Add(dRow[DistinctColumn]);
        }

        // Remove dupliate rows from DataTable added to DuplicateRecords
        foreach (DataRow dRow in DuplicateRecords)
        {
            table.Rows.Remove(dRow);
        }

        // Return the clean DataTable which contains unique records.
        return table;
    }
    catch (Exception ex)
    {
        return null;
    }
}

How to use this function:

DataTable DuplicateRecords = objDatabase.getTable("SQL Query");
DataTable UniqueRecords = RemoveDuplicateRows(DuplicateRecords, "Column Name to check for duplicate records");

The problem I found in the code is that the function is working for one distinct column only. What we do if we want to have more than one distinct column names in our query??

Eventually i got a solution to this which is smaller and faster than the above code. I hope this will answer to your query regarding removal of duplicate records in a DataTable:

var uniquerows = MsgsToSend2.AsEnumerable().Distinct(DataRowComparer.Default);

 DataTable Contacts = uniquerows.CopyToDataTable();

So the above two lines will serve your purpose. Here MsgsToSend2 is the DataTable containing the duplicate rows and the Contacts DataTable will have the unique rows. This is all about the advantage of LINQ (Language Integrated Query).




Comments

Popular posts from this blog

CDAC COURSES AND PLACEMENTS, WHICH IS BETTER FOR YOU

Click here to ask questions regarding CDAC
Before going through this post, I would like to draw your attention towards the importance of this post. This page not only explains my experience in CDAC but also aims at answering the queries of you all who are going or looking to have a course from CDAC. Kindly post your queries at the bottom of this page and we will get back to you within 24 hours. Kindly do not post your queries as an Anonymous user and do not forget to subscribe via email so as to keep track of your query.

                        click below to know about the CCAT's This batch allotment                 click below to know about the CCAT's This batch allotment ______________________________________________
NOW a day lot of the graduates and post graduates are wondering most of the times on which course they should go for. What are the pros & cons of joining a particular centre of CDAC. Here is a comprehensive information on how to enroll ourselves for a cour…

CDAC CCAT Rank - Which Centre you Should go for...

Click here to ask questions regarding CDAC
1. C-DAC (Head Quarters) Pune 
CDAC's Admission Booklet- Process of Admission to Post Graduate Diploma Courses of C-DAC                         click below to know about the CCAT's This batch allotment
               click above to know about the CCAT's This batch allotmentRank 1-300
c-dac HQ has been the best from the start.

2. Sunbeam Pune

Rank 300-500
I got very positive feedback from my friends who are in c-dac banglure main campus,since there are many companies you may get more opportunities.

3. C-DAC Knowledge Park

Rank 400-700
It as very good faculty .Almost all the students get placed here every year.

4. C-DAC Hyderabad

Rank 200-1000 (It depends on the course which you select)
C-DAC hyderabad is very good for the course PG-DESD.
PG-DESD course is in hyderbad is better than pune HQ as per the past feedback.It as very good placement record in hyd for PG-DESD course.Rank for PG-DESD is 1-300.
PG-DAC as no good placement record from few yea…

Placement Statistics - 2011 & 2012

Post your queries below and we will get back to you in no more than 24 hours.  Click here to ask questions regarding CDAC                         click below to know about the CCAT's This batch allotment                     click above to know about the CCAT's This batch allotment
CDAC's Admission Booklet- Process of Admission to Post Graduate Diploma Courses of C-DAC

Click here to see placement statistics

About C-CAT, Exam Pattern and Books

No. of Seats Across Various Training Centres

Important Dates - 2014








Tags: CDAC, CDAC scope in future, CDAC placements, CDAC training, CDAC recruitment, CDAC training centres, DSSD, DESD, DAC, PGDSSD, PGDESD, PGDAC, DABC, PGDABC, VLSI, PGDITISS, PGDIVESD, PGDESD, PGDWiMC, placement statistics