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: ,,

评论

《 “Remove Duplicate Records in a DataTable the Easy Way” 》 有 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. Dadapeer 的头像
    Dadapeer

    Excellent! Thank you…

  3. Saji 的头像
    Saji

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

  4. Saji 的头像
    Saji

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

  5. Christopher 的头像
    Christopher

    BINGO!! thanks 4 years later and still helping people LOL

  6. […] Much better but only allows duplicates from a single column which has been defined. The above code is simple and quick and I was about use it as a base when out of nowhere this appeared. […]

  7. […] Much better but only allows duplicates from a single column which has been defined. The above code is simple and quick and I was about use it as a base when out of nowhere this appeared. […]

  8. […] Much better but only allows duplicates from a single column which has been defined. The above code is simple and quick and I was about use it as a base when out of nowhere this appeared. […]

  9. […] Much better but only allows duplicates from a single column which as been defined. The above code is simple and quick and I was about use this as a base when out of nowhere this appeared. […]

  10. Debasmit 的头像
    Debasmit

    Excellent. thanks a lot.

  11. Guest 的头像
    Guest

    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

    1. Guest12345 的头像
      Guest12345

      this method will not work in your scenario

      http://msdn.microsoft.com/en-us/library/a8ycds2f.aspx

  12. tommy 的头像
    tommy

    ur a phenom thanks bro really helped!!

  13. excellent

  14. Neshu Awasthi 的头像
    Neshu Awasthi

    it is excellent thank u .

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

  15. Paolo 的头像
    Paolo

    Short, clean and elegant code.
    Thanks

  16. Guest 的头像
    Guest

    Thank you! That was so helpful

  17. Russ 的头像
    Russ

    Excellent!

  18. DevMark 的头像
    DevMark

    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!

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

  20. P.R. 的头像
    P.R.

    Nice!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注