【问题标题】:How to get required collection in my LINQ如何在我的 LINQ 中获取所需的集合
【发布时间】:2017-06-06 22:59:41
【问题描述】:

我的数据库中有一个表。

`tblCustomers`
    - CustomerId 
    - CustomerName 

现在我需要将所有客户按CustomerName 分组,但至少应该不止一次出现customername

如果有 10 个客户的 name = "ABC",将他们放入一个数组,但 1 个 customername = "XYZ"(只有一个具有此名称的记录)不要得到这个。

这就是我的想法

var query = (from c in entities.tblCustomers
group c.CustomerName into cust 
//where cust.Key.Count >=2  (how to get this)
select ABC()
{
});

【问题讨论】:

标签: c# asp.net linq


【解决方案1】:

可能是这样的:

from c in entities.tblCustomers
group c by c.CustomerName into cust 
where cust.Count() > 1
select cust.Key

相当于SQLHAVING子句。

【讨论】:

    【解决方案2】:

    使用方法运算符,它会是这样的:

    entities.tblCustomers
        .GroupBy(c => c.CustomerName)
        .Where(grp => grp.Count() > 1)
        .Select(grp => grp.Key);
    

    【讨论】:

      猜你喜欢
      • 2021-06-29
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 2010-09-24
      • 2022-11-11
      • 2011-11-19
      相关资源
      最近更新 更多