4

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

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;

但是,teams 集合不允许您像引用集合一样引用特定属性。我想说的是“选择团队集合中所有具有 TeamID 的代表”。

4

4 回答 4

7
var TeamDelegates = from tds in db.J_TeamDelegates
                    where teams.Any(x => x.TeamID == tds.TeamID)
                    select tds;
于 2013-02-20T17:50:12.650 回答
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;
于 2013-02-20T17:59:11.243 回答
1
 var TeamDelegates = db.Teams
                 .Where(tms => tms.DepartmentID == DepartmentID)
                 .SelectMany(tms => db.J_TeamDelegates
                                      .Where(tds => tds.TeamID == tms.TeamID))
于 2013-02-20T17:57:07.953 回答
1
var delegates = db.Departments
    .Where(department => department.ID == 123)
    .SelectMany(department => department.Teams)
    .SelectMany(team => team.Delegates);
于 2013-02-20T18:06:20.930 回答