Click here to Skip to main content
Click here to Skip to main content
Technical Blog

C#.NET – Removing Duplicate Records From DataTable

, 13 Feb 2013 CPOL
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.


If a database table contain duplicate records the 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.


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


For removing duplicate records from DataTable, I have create the below funciton 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 contain duplicate records:

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

        // Remove dupliate rows from DataTable added to DuplicateRecords
        foreach (DataRow dRow in DuplicateRecords)

        // 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


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:



The above function is selecting the first record and deleting others if duplicate record is found in DataTable.


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

Feel free to comment, suggest or give your feedback.

The post C#.NET – Removing Duplicate Records From DataTable appeared first on Noor Ahmad Feroozi.


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


About the Author

Noor Ahmad Feroozi
Software Developer (Senior) Ministry of Education
Afghanistan Afghanistan
Over eight years of IT industry experience, and have been a part of design and development projects for many exceptional companies during those years, including Kabul Cyber Park, Alpha Omega Services, Ministry of Defense, Spacetech ICT Solutions, Ministry of Education and many others.
Technologies I use are standards-based XHTML, CSS, JavaScript, jQuery, PHP, ASP.Net, C#.Net, MS SQL, MySQL and SQLite. Each project I develop is built to be search engine and user friendly, using the latest development principles and techniques. To find out more about what I can do for you, check out my work at, or get follow me on http://twitter/naferoozi
Follow on   Twitter   Google+

Comments and Discussions

Questionthanks Pinmemberloveleshsgsits19-Mar-15 8:02 
QuestionAnother simple and fast method for RemoveDuplicatesRecords Pinmembersatindersinght6-Mar-13 20:42 
GeneralMy vote of 5 Pinmemberbandi.ramesh16-Feb-13 0:17 
SuggestionWhat do you think about this codes? PinmemberLightEach14-Feb-13 15:04 
QuestionIs this column based? Pinmemberortund14-Feb-13 1:37 
QuestionHow about multiple column unique? Pinmemberjohnchen8813-Feb-13 9:59 
AnswerRe: How about multiple column unique? PinmemberNoor Ahmad Feroozi13-Feb-13 10:31 
AnswerRe: How about multiple column unique? PinmemberRichard Beatty20-Jan-15 8:56 
QuestionWorks perfectly PinmemberGorata11-Feb-13 0:30 
SuggestionAlternative technique 2 PinmemberRogerDW8-Feb-13 6:04 
GeneralRe: Alternative technique 2 Pinmvpthatraja4-Feb-14 2:52 
SuggestionAlternative technique 1 PinmemberRogerDW8-Feb-13 6:00 

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

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

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