0

I've found this piece of code that can be used to get all distinct values. But my datatable has 10 columns. The distinctValues only shows the columns I write in the toTable(); Is it possible to use this function, but also show the rest of the columns?

DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2");
Rocshy
  • 3,391
  • 11
  • 39
  • 56

2 Answers2

1

Unless those columns you mention are the full key to the table, there is no guarantee that for a particular combination of those two columns the other columns will have exactly one value.

And if they were the key, then there would be no need to use a "distinct" filter.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
0

You can use Linq-To-DataTable

var distinct = from row in table.AsEnumerable()
               group row by new
               {
                   Col1 = row.Field<string>("Column1"),
                   Col2 = row.Field<string>("Column2")
               } into Group
               select Group.First()
DataTable tblDistinct = distinctRows.CopyToDataTable();

(assuming that you just want an arbitrary row[the first])

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939