I have a DataTable
which looks like this:
ID Name DateBirth
.......................
1 aa 1.1.11
2 bb 2.3.11
2 cc
I was solving the same situation and found it quite interesting and would like to share my finding.
DataTable newDatatable = dt.DefaultView.ToTable(true, "ID", "Name", "DateBirth");
The columns you mention here, only those will be returned back in newDatatable
.
LINQ
query. DataTable newDatatable = dt.AsEnumerable()
.GroupBy(dr => dr.Field("ID"))
.Select(dg => dg).Take(1)
.CopyToDataTable();
List toExclude = new List();
for (int i = 0; i < dt.Rows.Count; i++)
{
var idValue = (string)dt.Rows[i]["ID"];
if (toExclude.Contains(idValue))
{
dt.Rows.Remove(dt.Rows[i]);
i--;
}
toExclude.Add(glAccount);
}
Third being my favorite.
I may have answered few things which are not asked in the question. It was done in good intent and with little excitement as well.
Hope it helps.