Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:41:28+00:00 2026-05-11T06:41:28+00:00

I’m trying EF out and I do a lot of filtering based on many

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:41:28+00:00Added an answer on May 11, 2026 at 6:41 am

    In Lambda :

        var persons = Persons.Where(p=>(p.PersonLocations.Select(ps=>ps.Location)    .Where(l=>l.Description == 'Amsterdam').Count() > 0)     && (p.PersonRoles.Select(pr=>pr.Role)    .Where(r=>r.Description == 'Student').Count() > 0)); 

    query result:

    SELECT [t0].[personId] AS [PersonId], [t0].[description] AS [Description] FROM [Persons] AS [t0] WHERE (((     SELECT COUNT(*)     FROM [personlocations] AS [t1]     INNER JOIN [Locations] AS [t2] ON [t2].[locationid] = [t1].[locationid]     WHERE ([t2].[description] = @p0) AND ([t1].[personid] = [t0].[personId])     )) > @p1) AND (((     SELECT COUNT(*)     FROM [PersonRoles] AS [t3]     INNER JOIN [Roles] AS [t4] ON [t4].[roleid] = [t3].[roleid]     WHERE ([t4].[description] = @p2) AND ([t3].[personid] = [t0].[personId])     )) > @p3) 

    Using Contains():

    var persons = Persons             .Where(p=>(p.Personlocations.Select(ps=>ps.Location)             .Select(l=>l.Description).Contains('Amsterdam')) &&              (p.PersonRoles.Select(pr=>pr.Role)             .Select(r=>r.Description).Contains('Student'))); 

    query result:

    SELECT [t0].[personId] AS [PersonId], [t0].[description] AS [Description] FROM [Persons] AS [t0] WHERE (EXISTS(     SELECT NULL AS [EMPTY]     FROM [personlocations] AS [t1]     INNER JOIN [Locations] AS [t2] ON [t2].[locationid] = [t1].[locationid]     WHERE ([t2].[description] = @p0) AND ([t1].[personid] = [t0].[personId])     )) AND (EXISTS(     SELECT NULL AS [EMPTY]     FROM [PersonRoles] AS [t3]     INNER JOIN [Roles] AS [t4] ON [t4].[roleid] = [t3].[roleid]     WHERE ([t4].[description] = @p1) AND ([t3].[personid] = [t0].[personId])     )) 

    using join():

    var persons = Persons         .Join(Personlocations, p=>p.PersonId, ps=>ps.Personid, (p,ps) => new {p,ps}) .Where(a => a.ps.Location.Description =='Amsterdam')         .Join(PersonRoles, pr=> pr.p.PersonId, r=>r.Personid,(pr,r) => new {pr.p,r}) .Where(a=>a.r.Role.Description=='Student')         .Select(p=> new {p.p}); 

    Query Result:

    SELECT [t0].[personId] AS [PersonId], [t0].[description] AS [Description] FROM [Persons] AS [t0] INNER JOIN [personlocations] AS [t1] ON [t0].[personId] = [t1].[personid] INNER JOIN [Locations] AS [t2] ON [t2].[locationid] = [t1].[locationid] INNER JOIN [PersonRoles] AS [t3] ON [t0].[personId] = [t3].[personid] INNER JOIN [Roles] AS [t4] ON [t4].[roleid] = [t3].[roleid] WHERE ([t4].[description] = @p0) AND ([t2].[description] = @p1) 

    You may want test wich one is faster with large data.

    Good luck.

    Giuliano Lemes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.