Tuesday, June 15, 2010

Select DISTINCT rows from a DataSet using C#.NET

We have a DataSet ds like so:

recordID groupID value
1 100 a
2 100 d
3 220 g
4 333 j

So to select distinct groupIDs we simply:

DataTable distinctDT = ds.Tables[0].DefaultView.ToTable(true, new string [] { "groupID" });

This returns:
groupID
100
220
333