2

Possible Duplicate:
How to select distinct values from datatable?

Here is my code that I'm currently working on. I'm trying to get rid off the duplicated fields in my Dataset. I believe the correct way of doing this is by an If statement. Can anyone help please? This is the code I have so far:

Community
  • 1
  • 1
john
  • 41
  • 5

2 Answers2

2

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.

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

reference : Remove Duplicate Records in a DataTable the Easy Way

Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59
0

Every time I need uniqueness I use HashSet instead of other structures. So the short answer is :I don't.

Hashset has a nice feature of letting you decide what it thinks is unique - just use the appropriate constructor.

Another thing is - if you're not using DataSets to access the database - don't use it, and if you do then use the strongly typed version instead. This of course does not apply if you have good reasons to do so - it's just a general rule.

kubal5003
  • 7,186
  • 8
  • 52
  • 90