Saturday, 27 October 2012

Remove Duplicate Records in a DataTable


Remove Duplicate Records in a DataTable 

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.

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.

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.

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.

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top