简体   繁体   中英

Select distinct rows in c# datatable on single column but return all columns

DataView view = new DataView(dt);
dt= view.ToTable(true, "id");

If i have 2 columns "id" and "name" i need to get the distinct "id" but not the distinct "name" but i need to keep the column "name" in my datatable what shall i do?

DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2" ...);

If i have 2 columns "id" and "name" i need to get the distinct "id" but not the distinct "name" but i need to keep the column "name" in my datatable what shall i do?

OK, then i need grouping, not distinct.I could do it with Linq to DataSet: table.AsEnumerable().GroupBy(row => row.Field<int>("id")).Select(group =>group.First()).CopyToDataTable()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM