C# - Remove rows with the same column value from a DataTable

c#, datatable, filtering, linq

Solution

You can use LINQ to DataTable, to distinct based on column `ID`, you can group by on this column, then do select first:

  var result = dt.AsEnumerable()
                 .GroupBy(r => r.Field<int>("ID"))
                 .Select(g => g.First())
                 .CopyToDataTable();

Problem

I have a `DataTable` which looks like this: ``` ID Name DateBirth ....................... 1 aa 1.1.11 2 bb 2.3.11 2 cc 1.2.12 3 cd 2.3.12 ``` Which is the fastest way to remove the rows with the same ID, to get something like this (keep the first occurrence, delete the next ones): ``` ID Name DateBirth ....................... 1 aa 1.1.11 2 bb 2.3.11 3 cd 2.3.12 ``` I don't want to double pass the table rows, because the row number is big. I want to use some LinQ if possible, but I guess it will be a big query and I have to use a comparer.

Original source