【问题标题】:Linq grouping multiple where conditionLinq分组多个where条件
【发布时间】:2016-08-17 22:54:44
【问题描述】:

我有一个用 SQL 编写的查询,它有效,我正试图将其移至适当的 LINQ 语句,但似乎无法完全正确。

SQL 查询:

select sku
from Table
group by sku
having count(sku) > 1 and count(distinct(unit)) > 1

以及到目前为止我对 LINQ 的了解

    var dupCount = (from val in dt.AsEnumerable()
                    group val by new {sku = val[4]} into grp
                    where grp.Count() > 1 && grp.unit.Distinct().Count() > 1
                    select grp).ToList();

我似乎无法弄清楚如何告诉组内的哪个位置只提取具有不同“unitreserve”的记录。

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    我似乎无法弄清楚如何告诉组内的哪个位置只提取具有不同“unitreserve”的记录。

    Select "unitreverse" 字段,应用Distinct 然后Count 它:

    where grp.Count() > 1 && 
          grp.Select(dr => dr["unitreserve"]).Distinct().Count() > 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-07
      • 2014-05-30
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多