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

发布于 2024-12-01 12:02:38 字数 678 浏览 1 评论 0原文

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

I am trying to get the teams a person belongs to using this query. The problem is that it returns the teams of all the departments of the team the person is in (I get multiple identical records back). I guess I have to replace the .contains but I can't figure out with what as I'm a complete newb and I can't find any helpful examples with double joins. What do I have to change to make it work as intended? Thanks in advance.

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2024-12-08 12:02:39

尝试

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

try

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文