如何在Linq中使用分组和计数 [英] How to use group by and having count in Linq

查看:103
本文介绍了如何在Linq中使用分组和计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将以下查询从SQL转换为Linq时遇到了麻烦,尤其是在查询中具有count和group by的情况下:

I am having trouble trying to convert the following query from SQL to Linq, in particular with the having count and group by parts of the query:

select pa.ProjectID
from ProjectAssociation pa
where pa.TeamID in ( select TeamID
                    from [User]
                    where UserID in (4))
group by pa.ProjectID
having COUNT(pa.TeamID) = (select distinct COUNT(TeamID)
                           from [User]
                           where UserID in (4))


有关如何执行此操作的任何建议将不胜感激:)


Any advice on how to do so would be much appreciated :)

推荐答案

以下链接将指导您将查询转换为LINQ,并帮助您学习如何使用组并在LINQ中拥有

http://www.aspfree. com/c/a/.NET/Grouping-and-Aggregating-When-Querying-LINQ-to-SQL/4/ [ http://weblogs. asp.net/vikram/archive/2009/09/17/linq-having-clause-and-group-by-with-condition.aspx [ http://msdn.microsoft.com/en-us/vbasic/bb737926.aspx [ ^ ]

希望这会有所帮助!
Following links will guide you to convert your query into LINQ and help you to learn how to use group by and having in LINQ

http://www.aspfree.com/c/a/.NET/Grouping-and-Aggregating-When-Querying-LINQ-to-SQL/4/[^]

http://weblogs.asp.net/vikram/archive/2009/09/17/linq-having-clause-and-group-by-with-condition.aspx[^]

http://msdn.microsoft.com/en-us/vbasic/bb737926.aspx[^]

Hope this will help!


这篇关于如何在Linq中使用分组和计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆