Quantcast
Channel: Smartech Force Knowledge Share » Visual Studio.Net
Viewing all articles
Browse latest Browse all 5

Remove Duplicate Records in a DataTable the Easy Way

$
0
0

 

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.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images