gpt4 book ai didi

c# - Linq To SQL - 具有和分组依据

转载 作者:太空宇宙 更新时间:2023-11-03 16:09:27 25 4
gpt4 key购买 nike

下面的查询运行良好。但是我想使用 Linq 来实现它。

select u.ID, u.NAME
from Task t
join BuildingUser bu ON bu.ID_BUILDING = t.ID_BUILDING
join [User] u ON u.ID = bu.ID_USER
where t.ID IN (2,9) AND u.ID != t.ID_USER
group by u.id, u.name
having count(bu.ID_BUILDING) = (SELECT COUNT(t2.ID_BUILDING) FROM Task t2 WHERE t2.ID IN (2,9))

我不知道如何同时使用 Group 和 Having 子句。

最佳答案

你可以尝试这样的事情:

var ids = new[] { 2, 9 };
var results =
from t in db.Tasks
join bu in db.BuildingUsers on t.ID_BUILDING equals bu.ID_BUILDING
group bu by bu.ID_BUILDING into bg
join u in db.Users on bg.Key equals u.ID
where ids.Contains(t.ID) && u.ID != t.ID_USER
group u by new { u.ID, u.NAME } into g
where bg.Count() == db.Tasks.Count(t2 => ids.Contains(t2.ID))
select g.Key;

或者,如果您的导航属性设置正确,您可以试试这个:

var ids = new[] { 2, 9 };
var results =
from t in db.Tasks.Where(x => ids.Contains(x.ID))
from u in t.BuildingUsers.SelectMany(bu => bu.Users)
.Where(x => x.ID != t.ID_USER)
group u by new { u.ID, u.NAME } into g
where t.BuildingUsers.Count() == db.Tasks.Count(x => ids.Contains(x.ID))
select g.Key;

关于c# - Linq To SQL - 具有和分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18050386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com