1

I have a datatable that has duplicate ID's.

1 data moredata  4354
1 data moredata  4354
2 data moredaaata  4asdfdsa4
3 dasata edaaata  42342fdsa4
2 data moredaaata  4asdfdsa4

I want to get a datatable from that table with just row for each of the first column value like this: (does not need to be in order)

1 data moredata  4354
2 data moredaaata  4asdfdsa4
3 dasata edaaata  42342fdsa4

How can I do this?

4

3 回答 3

4

您可以使用 LINQ 来查询数据表。ID在字段上使用分组并First从结果集中进行选择。就像是..

var query = from t in dt.AsEnumerable()
            group t by t.Field<int>("ID") into g
            select g.First();
于 2012-07-30T19:37:33.273 回答
2
List<string> tableColumns = new List<string>();
foreach (DataColumn column in currentTable.Columns)
    tableColumns.Add(column.ColumnName);
DataTable result = currentTable.DefaultView.ToTable(true, tableColumns.ToArray());
于 2013-05-24T11:13:09.767 回答
0

SELECT DISTINCT Column1, Column 2 FROM Table1 WHERE ......

于 2012-07-30T21:56:00.547 回答