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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:58:29+00:00 2026-06-02T10:58:29+00:00

Is it possible using DbContext / DbSet / DbQuery to conditionally eagerly load a

  • 0

Is it possible using DbContext / DbSet / DbQuery to conditionally eagerly load a navigation property. The query below will return parties that have a particular role.

What I need is to additionally only load the matching roles.

first attempt

var peopleWithRole = (from p in Party
  from r in p.Roles
  where r.RoleTypeId == 1
  select p).Include(_ => _.Roles);

This loads all the roles, and it’s obvious why once you look at it.

I have tried a few things. If I have to cast and create an objectquery to do it that would be fine, I just can’t figure out how.

second attempt

var objectContext = ((IObjectContextAdapter)this).ObjectContext;
var set = objectContext.CreateObjectSet<Party>();
var result = (from p in set.Where("it.Roles.RoleTypeId == 1")
              select p)
;
result.Dump();

The iterator cannot be used like this?

Any ideas?

  • 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-02T10:58:35+00:00Added an answer on June 2, 2026 at 10:58 am

    Filtering and sorting with eager loading is not supported. The only way to load the data in a single roundtrip is a projection:

    var peopleWithRole = (from p in Party
                          select new
                          {
                              Party = p,
                              Roles = p.Roles.Where(r => r.RoleTypeId == 1)
                          })
                          .ToList()
                          .Select(a => a.Party);
    

    If the relationship is one-to-many EF (“relationship fixup”) will populate the filtered Roles collection automatically in each Party (if you don’t disable change tracking). If the relationship is many-to-many it doesn’t work that easy and you need to fill the collections manually after you have returned the result list of anonymous objects.

    Last option is to vote for the eager loading filtering feature here: http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1015345-allow-filtering-for-include-extension-method … and hope it will be implemented in future EF releases.

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

Sidebar

Related Questions

Is it possible using CAML to limit the amount of results returned. Say for
I know this is probably possible using Streams, but I wasn't sure the correct
I'm looking after a way to AlphaBlend a child form, if possible using layered
Possible Duplicate: Using ASP.NET Controls without databinding My previous question yielded few results so
Possible Duplicate: using bash: write bit representation of integer to file I need to
Possible Duplicate: Using SendMessage to enter text into an edit control belonging to another
Possible Duplicate: Using a bitwise & inside an if statement I have the following
Is it possible without using exponentiation to have a set of numbers that when
Is it possible when using TFS server to automatically have it ensure a specific
What is the best possible way using jQuery to give height and width to

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.