4

I have hierarchy wherein a Department contains Teams and Teams contain Delegates. What I'm trying to do is get a list of Delegates that exist under a given Department. I tried doing it this way:

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; 

But the teams collection doesn't allow you to refer to a particular property as if it were a collection. What I'm trying to say is "Select all the Delegates with TeamIDs in the teams collection."

0

4 Answers 4

7
var TeamDelegates = from tds in db.J_TeamDelegates where teams.Any(x => x.TeamID == tds.TeamID) select tds; 
Sign up to request clarification or add additional context in comments.

1 Comment

Although this directly answers the question, I think it's worth noting that the way I expressed it (with two SelectMany calls) is much more straightforward.
2

I think you can use a join here.

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; 

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.