Click here to Skip to main content
13,043,898 members (52,061 online)
Click here to Skip to main content
Add your own
alternative version

Stats

59.5K views
12 bookmarked
Posted 7 Feb 2013

C#.NET – Removing Duplicate Records From DataTable

, 13 Feb 2013
Rate this:
Please Sign up or sign in to vote.
A function to remove and select unique records from a DataTable in C#.NET and return a clean and duplicate-free DataTable

Introduction

If a database table contains duplicate records, then it is easy to select unique records by using DISTINCT function, but when a .NET DataTable contains duplicate records, then there is no built-in function to remove or select unique records from a DataTable.

Objective

The main goal of this article is to create a function and remove and select unique records from a DataTable in C#.NET and return a clean and duplicate free DataTable.

Explanation

For removing duplicate records from DataTable, I have created the below function which needs two parameters, DataTable and Column Name. This function is searching for duplicate records in DataTable, given column name from second parameter and adding the row (record) in an ArrayList variable. Then, the record will be deleted from the datatable by running a loop on founded duplicate records.

DataTable containing duplicate records:

duplicates

Function for Removing Duplicate Records from DataTable

/// <summary>
/// Remove duplicate records from data table
/// </summary>
/// <param name="table">DataTable for removing duplicate records</param>
/// <param name="DistinctColumn">Column to check for duplicate values or records</param>
/// <returns></returns>
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 duplicate 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;
    }
}

Using the Function

It is very easy to use this function, all you need is to pass the datatable and column name parameters to the function and the function returns the cleaned datatable.

Example

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

Clean and duplicate free DataTable using the above function:

cleaned

Remarks

The above function selects the first record and deletes others if a duplicate record is found in DataTable.

Conclusion

By using this function, you will be able to clean your datatable from duplicate records.

Feel free to comment, suggest or give your feedback.

License

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

Share

About the Author

Noor Ahmad Feroozi
Software Developer (Senior) Ministry of Education
Afghanistan Afghanistan
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionRemove white space Pin
Andy0720-Mar-17 19:07
memberAndy0720-Mar-17 19:07 
Questionthanks Pin
loveleshsgsits19-Mar-15 7:02
memberloveleshsgsits19-Mar-15 7:02 
GeneralMy vote of 5 Pin
bandi.ramesh15-Feb-13 23:17
memberbandi.ramesh15-Feb-13 23:17 
SuggestionWhat do you think about this codes? Pin
LightEach14-Feb-13 14:04
memberLightEach14-Feb-13 14:04 
QuestionIs this column based? Pin
ortund14-Feb-13 0:37
memberortund14-Feb-13 0:37 
QuestionHow about multiple column unique? Pin
johnchen8813-Feb-13 8:59
memberjohnchen8813-Feb-13 8:59 
AnswerRe: How about multiple column unique? Pin
Noor Ahmad Feroozi13-Feb-13 9:31
memberNoor Ahmad Feroozi13-Feb-13 9:31 
AnswerRe: How about multiple column unique? Pin
Richard Beatty20-Jan-15 7:56
memberRichard Beatty20-Jan-15 7:56 
QuestionWorks perfectly Pin
Gorata10-Feb-13 23:30
memberGorata10-Feb-13 23:30 
SuggestionAlternative technique 2 Pin
RogerDW8-Feb-13 5:04
memberRogerDW8-Feb-13 5:04 
GeneralRe: Alternative technique 2 Pin
thatraja4-Feb-14 1:52
mvpthatraja4-Feb-14 1:52 
SuggestionAlternative technique 1 Pin
RogerDW8-Feb-13 5:00
memberRogerDW8-Feb-13 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 13 Feb 2013
Article Copyright 2013 by Noor Ahmad Feroozi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid