获取行中值为Count的不同行>;1.

本文关键字:gt Count 获取 | 更新日期: 2023-09-27 18:25:00

到目前为止,我有这个

var GetSetRows = from DataRow dr in MyTable.Rows
    group dr by dr.Field<string>("SetNumber") into grp
    from rows in grp
    where grp.Count() > 1
    select rows;

并且这用于返回SetNumber在另一行中的每一行。

如何将其修改为每个计数>1的SetNumber只返回1行?

获取行中值为Count的不同行>;1.

使用Enumerable.First:

var GetSetRows = from DataRow dr in MyTable.Rows
                 group dr by dr.Field<string>("SetNumber") into grp
                 where grp.Count() > 1
                 select grp.First();
var GetSetRows = MyTable.AsEnumerable()
                        .GroupBy(r => r.Field<string>("SetNumber"))
                        .Where(g => g.Count() > 1)
                        .Select(g => g.First());
var GetSetRows = MyTable.Rows.GroupBy(x => x.Field<string>("SetNumber"))
                    .Where(g => g.Count() > 1)
                    .Select(g => g.First());