views:

43

answers:

1

I have Two Many-To-Many tables which I need to write a Entity Framework Linq query for. All I want to do is to retrieve any People from the "People" table who have a relation to the Company ID = 7.

Can someone give me an idea of what that would look like?

The tables look something like:

People, ID PK, Title, Forename, Surname

People_Companies PersonID PK, CompanyID PK,

Companies ID PK, CompanyNumber, CompanyName, CompanyType,

The SQL looks like this:

SELECT     People_Companies.PersonID, People_Companies.CompanyID,Companies.ID,Companies.CompanyNumber, Companies.CompanyName, 
                  Companies.CompanyType, People.ID AS Expr1, People.Title, People.Forename, People.Surname, People_Companies.PersonID AS Expr2, People_Companies.CompanyID AS Expr3, Companies.ID AS Expr4, Companies.CompanyNumber AS Expr5, Companies.CompanyName AS Expr6
FROM         People INNER JOIN
                  People_Companies ON People.ID = People_Companies.PersonID INNER JOIN
                  Companies ON People_Companies.CompanyID = Companies.ID
WHERE     (People_Companies.CompanyID = 7)
+1  A: 

try

var q = context.People.Where(p => p.Companies.Any(c => c.CompanyId == 7)).ToList();
moi_meme
Thanks, that was exactly what I needed
Paul Connolly