你可以尝试这样的事情:
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;