Remove Duplicate Records in a DataTable the Easy Way

The Merge method of DataTable class is convenient for combining two tables into one. However, the result table may contain duplicate data after the operation. We could write our own function to remove the duplicates, or, we could take advantage of a built-in method of DataView class.

Method Intro

There is this DataView method called ToTable with two parameters: (and a three-parameter overloaded version)

a boolean param distinct
If true, the returned System.Data.DataTable contains rows that have distinct values for all its columns. The default value is false.

a string array param columnNames
A string array that contains a list of the column names to be included in the returned System.Data.DataTable. The System.Data.DataTable contains the specified columns in the order they appear within this array.

Thought

We could first create a DataView object dv using the source table dt, then we turn dv into the destination "slim" DataTable dt through that ToTable method introduced above. The redundant data will be removed automatically.

Coding

Let’s assume dt is the source DataTable object with duplicate records.

// create a dv from the source dt
DataView dv = new DataView(dt);
// set the output columns array of the destination dt
string[] strColumns = {"NodeID", "Title", "Url"};
// true = yes, i need distinct values.
dt = dv.ToTable(true, strColumns);

That’s it, the easy way to get redundant data removed from a DataTable object.

Technorati Tags: ,,

作者:Keefe Dunn

a dreamer, a learner, a lawful beginner.

21条评论

  1. Heres easy and best way to remove duplicate without using for loop.
    private DataTable RemoveDuplicatesRecords(DataTable dt)
    {
    //Returns just 5 unique rows
    var UniqueRows = dt.AsEnumerable().Distinct(DataRowComparer.Default);
    DataTable dt2 = UniqueRows.CopyToDataTable();
    return dt2;
    }

    For More DETAIL

  2. dtEmp ur current working DataTable
    DataTable distinctTable = dtEmp.DefaultView.ToTable( /*distinct*/ true);

  3. dtEmp is ur working datatable
    DataTable distinctTable = dtEmp.DefaultView.ToTable( /*distinct*/ true);

  4. Thanks for superb answer…
    ..
    ..
    ..
    Still I want to remove duplicate value for specific field(ID). So, Is there any way to remove duplicate record on that field…
    ..
    ..
    thnks in advnc

  5. it is excellent thank u .

    Thanks a lot for the information, this was exactly what I needed..

  6. Brilliant. Searched for this for a while and found loads of examples with superfluous code iterating through the tables. I knew there had to be a better way – well done on finding it!

发表评论

您的电子邮箱地址不会被公开。