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 9191535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:42:27+00:00 2026-06-17T20:42:27+00:00

This is my real world example. I have 4 tables: Person Plan Coverage CoveredMembers

  • 0

This is my real world example.
I have 4 tables:

  • Person
  • Plan
  • Coverage
  • CoveredMembers

Each person can have many plans, each of those plans can have many coverages. Each of those coverages can have many CoveredMembers.

I need a query that will apply a filter on Plan.PlanType == 1 and CoveredMembers.TermDate == null.
This query should bring back any person who has a medical type plan that is not terminated.

This SQL statement would do just that:

SELECT Person.*, Plans.*, Coverages.*, CoveredMembers.* 
FROM Person P 
INNER JOIN Plan PL ON P.PersonID = PL.PersonID 
INNER JOIN Coverage C on PL.PlanID = C.PlanID 
INNER JOIN CoveredMember CM on C.CoverageID = CM.CoverageID 
WHERE CM.TermDate = NULL AND PL.PlanType = 1

I have figured out how to do this using anonymous types, but I sometimes need to update the data and save back to the database – and anonymous types are read only.

I was given a solution that did work using JOIN but it only brought back the persons (albeit filtered the way I needed). I can then loop through each person:

foreach (var person in persons) {
  foreach (var plan in person.Plans{
    //do stuff 
  }
}

But wouldn’t that make a db call for each iteration of the loop? I have 500 persons with 3 unterminated medical plans each, so it would call the db 1500 times?

This is why I want to bring the whole data tree from Persons to CoveredMembers back in one shot. Is this not possible?

  • 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. Editorial Team
    Editorial Team
    2026-06-17T20:42:28+00:00Added an answer on June 17, 2026 at 8:42 pm

    I believe this is accomplished in two parts:

    1. Your query to determine the people you wish to have returned based on your criteria as discussed in this question previously: Entity framework. Need help filtering results

    2. Properly setting the navigation properties for entities you want brought together to be eagerly loaded: http://msdn.microsoft.com/en-us/data/jj574232.aspx

    For example if your Person entity looks like:

    public class Person {
       public List<Plan> Plans {get; set;}
       ...
    }
    

    When returning data from the dbcontext you can also use explicit eager loading with the include option:

     var people = context.People
             .Include(p => p.Plans)
             .ToList();
    ....
    

    If these are nested – coverage is part of plan, etc (which it looks like, it goes something like):

     var people = context.People
             .Include(p => p.Plans.Select(pl=>pl.Coverage).Select(c=>c.CoveredMembers)))
             .ToList();
    ....
    

    I am making some assumptions about your data model here, and my code above probably needs a little tweaking.

    EDIT:

    I might need someone else to weigh in here, but I don’t think you can add the where clause into an include like that (my example above leads you that way a bit by putting the include on the context object, instead return an IQueryable with your conditions set as solved in your first post (without a ToList() called on it) and then use the code you wrote above without the Where clauses:

    From first post (you supplied different criteria in this one, but same concept)

     var q = from q1 in dbContext.Parent
        join q2 in dbContext.Children
        on q1.key equals q2.fkey
        join q3 in  ........
        where q4.col1 == 3000
        select q1;
    

    Then:

    List<Person> people = q.Include(p => p.Plans
      .Select(pl => pl.Coverages)
      .Select(c => c.CoveredMembers).ToList();
    

    Again, doing this without being able to troubleshoot – I am sure it would take me a few attempts to iron this one out too.

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

Sidebar

Related Questions

I have a real world program that is similar to this one, which I'll
I'm going to get some real-world data the best I can for this query
Currently using MySQL version 5.1.6 This is my first real world build and so
...before everything, I'm doing this out of curiosity only. Nothing real-world application here, but
This is a real newbie question. I have simple app that selects a picture
So this should be a real easy question but I can't seem to find
How would I use active record in this case: I have two tables: Users,
I have two tables, and am doing an ordered select on each of them.
Here is a simplified (NOT real world) example. Suppose there is a domain model
I have to take an T-SQL exam. I will be given a real world

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.