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

  • Home
  • SEARCH
  • 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 6601205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:43:22+00:00 2026-05-25T18:43:22+00:00

This is the second step of a question explained here: EF 4.1 code-first: How

  • 0

This is the second step of a question explained here: EF 4.1 code-first: How to load related data (parent-child-grandchild)?.
With @Slauma‘s guidance, I have successfully retrieved data with this approach:

var model = DbContext.SitePages
    .Where(p => p.ParentId == null && p.Level == 1)
    .OrderBy(p => p.Order) // ordering parent 
    .ToList();

foreach (var child in model) { // loading children
    DbContext.Entry(child)
        .Collection(t => t.Children)
        .Query()
        .OrderBy(t => t.Order) // ordering children
        .Load();

    foreach (var grand in child.Children) { // loading grandchildren
        DbContext.Entry(grand)
            .Collection(t => t.Children)
            .Query()
            .OrderBy(t => t.Order) // ordering grandchildren 
            .Load();
    }
}

Though this approach works, it sends many queries to the database and I am searching for a way to do this all in just one query. With @Slauma‘s guidance (explained in the answer at the above link), I have changed the query to this one:

var model2 = DbContext.SitePages
    .Where(p => p.ParentId == null && p.Level == 1)
    .OrderBy(p => p.Order)
    .Include(p => p.Children // Children: how to order theme???
        .Select(c => c.Children) // Grandchildren: how to order them???
    ).ToList();

Now, how can I order children (and grandchildren) when selecting them (such as shown in the first code example above)?

  • 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-05-25T18:43:23+00:00Added an answer on May 25, 2026 at 6:43 pm

    Unfortunately eager loading (Include) doesn’t support any filtering or sorting of loaded child collections. There are three options to achieve what you want:

    • Multiple roundtrips to the database with explicite sorted loading. That’s the first code snippet in your question. Be aware that multiple roundtrips are not necessarily bad and that Include and nested Include can lead to huge multiplication of transfered data between database and client.

    • Use eager loading with Include or Include(....Select(....)) and sort the data in memory after they are loaded:

      var model2 = DbContext.SitePages
          .Where(p => p.ParentId == null && p.Level == 1)
          .OrderBy(p => p.Order)
          .Include(p => p.Children.Select(c => c.Children))
          .ToList();
      
      foreach (var parent in model2)
      {
          parent.Children = parent.Children.OrderBy(c => c.Order).ToList();
          foreach (var child in parent.Children)
              child.Children = child.Children.OrderBy(cc => cc.Order).ToList();
      }
      
    • Use a projection:

      var model2 = DbContext.SitePages
          .Where(p => p.ParentId == null && p.Level == 1)
          .OrderBy(p => p.Order)
          .Select(p => new
          {
              Parent = p,
              Children = p.Children.OrderBy(c => c.Order)
                  .Select(c => new
                  {
                      Child = c,
                      Children = c.Children.OrderBy(cc => cc.Order)
                  })
          })
          .ToList() // don't remove that!
          .Select(a => a.Parent)
          .ToList();
      

    This is only a single roundtrip and works if you don’t disable change tracking (don’t use .AsNoTracking() in this query). All objects in this projection must be loaded into the context (the reason why the first ToList() is necessary) and the context will tie the navigation properties correctly together (which is a feature called “Relationship span”).

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

Sidebar

Related Questions

This is my second question on Bamboo ( My First One ). My understanding
This is a second-hand question from an OS development site, but it made me
I hope it's the heat, this is the second question today and it's one
This bit of code is taking almost a half second to execute. Could somebody
In this particular code sample I want to reference the second overloaded method (int
This is a two part question. First how can I grab the last url
Similar to this question , but taking it a step further. I would like
First off this isn't for any homework question, it's just on a general type
I've got a Perl script that needs to execute another Perl script. This second
I've a application that must work after another application. This second application has a

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.