得到行中的值是计数的不同行> 1?

时间:2013-07-26 14:59:50

标签: c# linq distinct

到目前为止,我有这个

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在另一行中的每一行。

如何将此修改为仅为每个具有Count&gt;的SetNumber返回1行? 1?

3 个答案:

答案 0 :(得分:4)

使用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();

答案 1 :(得分:1)

var GetSetRows = MyTable.Rows.GroupBy(x => x.Field<string>("SetNumber"))
                    .Where(g => g.Count() > 1)
                    .Select(g => g.First());

答案 2 :(得分:1)

var GetSetRows = MyTable.AsEnumerable()
                        .GroupBy(r => r.Field<string>("SetNumber"))
                        .Where(g => g.Count() > 1)
                        .Select(g => g.First());