双连接查询返回多个相同的记录而不是唯一的记录

时间:2011-08-27 19:16:07

标签: linq-to-sql

我正在尝试让一个人所属的团队使用此查询。问题是它返回了该团队所有部门的团队(我得到了多个相同的记录)。我想我必须更换.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;
    }

1 个答案:

答案 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();