我正在尝试让一个人所属的团队使用此查询。问题是它返回了该团队所有部门的团队(我得到了多个相同的记录)。我想我必须更换.contains,但我无法弄明白我是一个完整的新手,我找不到任何有用的双连接示例。我需要更改什么才能使其按预期工作?提前谢谢。
public IQueryable<Team> GetTeamsByPersonID(int id)
{
return from t in entities.Teams
join d in entities.Departments
on t.TeamID equals d.TeamID
where (from p in entities.Person_Departments
join dep in entities.Departments
on p.DepartmentID equals dep.DepartmentID
where p.PersonID == id
select dep.TeamID).Contains(d.TeamID)
select t;
}
答案 0 :(得分:4)
试
return (
from t in entities.Teams
from d in entities.Departments
from p in entities.Person_Departments
where t.TeamID == d.TeamID && p.DepartmentID == d.DepartmentID && p.PersonID == id
select t
).Distinct();