【问题标题】:Linq with group by having count通过计数与组进行 Linq
【发布时间】:2011-01-05 22:19:21
【问题描述】:

如何在 linq (vb.net) 中编写此查询?

 select B.Name
 from Company B
 group by B.Name
 having COUNT(1) > 1

【问题讨论】:

标签: linq count group-by having


【解决方案1】:

像这样:

from c in db.Company
group c by c.Name into grp
where grp.Count() > 1
select grp.Key

或者,使用方法语法:

Company
    .GroupBy(c => c.Name)
    .Where(grp => grp.Count() > 1)
    .Select(grp => grp.Key);

【讨论】:

  • 感谢您提供两种形式的语法! :D
  • 感谢您的解决方案
  • 简单准确,谢谢
【解决方案2】:

对于希望在 vb 中执行此操作的任何人(就像我一样,但找不到任何东西)

From c In db.Company 
Select c.Name Group By Name Into Group 
Where Group.Count > 1

【讨论】:

  • 我很难理解为什么Group By 在VB 中的Select 子句之后。
【解决方案3】:

以下解决方案可能会对您有所帮助。

var unmanagedDownloadcountwithfilter = from count in unmanagedDownloadCount.Where(d =>d.downloaddate >= startDate && d.downloaddate <= endDate)
group count by count.unmanagedassetregistryid into grouped
where grouped.Count() > request.Download
select new
{
   UnmanagedAssetRegistryID = grouped.Key,
   Count = grouped.Count()
};

【讨论】:

  • 这与问题有什么关系?
猜你喜欢
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 2013-01-29
  • 2013-04-29
  • 2016-08-08
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
相关资源
最近更新 更多