Linq在哪里

时间:2013-02-20 17:48:07

标签: c# .net linq

我有一个层次结构,其中一个部门包含团队和团队包含代表。我要做的是获取一个给定部门下存在的代表列表。我试着这样做:

var teams = from tms in db.Teams
            where tms.DepartmentID == DepartmentID
            select tms;

var TeamDelegates = from tds in db.J_TeamDelegates
                    where tds.TeamID in teams.TeamID //error here
                    select tds;

但是团队集合不允许您将特定属性称为集合。我想说的是“在团队集合中选择所有具有TeamID的代表。”

4 个答案:

答案 0 :(得分:7)

var TeamDelegates = from tds in db.J_TeamDelegates
                    where teams.Any(x => x.TeamID == tds.TeamID)
                    select tds;

答案 1 :(得分:2)

我认为你可以在这里使用联接。

var TeamDelegates = from tms in db.Teams
                    where tms.DepartmentID == DepartmentID
                    join tds in db.J_TeamDelegates on tms.TeamID equals tds.TeamID
                    select tds;

答案 2 :(得分:1)

 var TeamDelegates = db.Teams
                 .Where(tms => tms.DepartmentID == DepartmentID)
                 .SelectMany(tms => db.J_TeamDelegates
                                      .Where(tds => tds.TeamID == tms.TeamID))

答案 3 :(得分:1)

var delegates = db.Departments
    .Where(department => department.ID == 123)
    .SelectMany(department => department.Teams)
    .SelectMany(team => team.Delegates);