Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq where in clause

Tags:

c#

.net

linq

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."

like image 438
Legion Avatar asked Dec 20 '22 09:12

Legion


2 Answers

var TeamDelegates = from tds in db.J_TeamDelegates
                    where teams.Any(x => x.TeamID == tds.TeamID)
                    select tds;
like image 104
gzaxx Avatar answered Dec 22 '22 22:12

gzaxx


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;
like image 32
gunr2171 Avatar answered Dec 22 '22 22:12

gunr2171