I’m trying EF out and I do a lot of filtering based on many to many relationships. For instance I have persons, locations and a personlocation table to link the two. I also have a role and personrole table.
EDIT: Tables: Person (personid, name) Personlocation (personid, locationid) Location (locationid, description) Personrole (personid, roleid) Role (roleid, description)
EF will give me persons, roles and location entities. EDIT: Since EF will NOT generate the personlocation and personrole entity types, they cannot be used in the query.
How do I create a query to give me all the persons of a given location with a given role?
In SQL the query would be
select p.* from persons as p join personlocations as pl on p.personid=pl.personid join locations as l on pl.locationid=l.locationid join personroles as pr on p.personid=pr.personid join roles as r on pr.roleid=r.roleid where r.description='Student' and l.description='Amsterdam'
I’ve looked, but I can’t seem to find a simple solution.
In Lambda :
query result:
Using Contains():
query result:
using join():
Query Result:
You may want test wich one is faster with large data.
Good luck.
Giuliano Lemes