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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:36:31+00:00 2026-06-09T14:36:31+00:00

I need to retrieve data using EF 4.1 Code first that are contained in

  • 0

I need to retrieve data using EF 4.1 Code first that are contained in several related tables. Currently if i use this

return Context.Set<Entity>()
              .Include(x => x.Children.Select(y => y.GrandChildren.Select(z => z.Child)))
              .Include(x => x.SomeEntity)
              .Include(x => x.AnotherEntity)
              .OrderByDescending(x => x.Id)
              .FirstOrDefault();

The data is fetched correctly, but I am worried about two things:

1) it seems that there is no way to sort Chilren / GrandChildren

2) the data is flattened in one table, which means that Entity (and all others) data is duplicated for each GrandChild record

Questions:

  • What Do I need to do to be able to sort Children / GrandChildren?
  • The 2nd point itself may not be a problem in this particular case, since the amount of data that is transferred is not big – max 30 records with 30 columns. Still, I would like to know if there is a way to load Enttity, Children and GrandChildren separately (3 queries), and join them on client site?

Returned resultset needs to be updatable.

  • 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-09T14:36:33+00:00Added an answer on June 9, 2026 at 2:36 pm

    What Do I need to do to be able to sort Children / GrandChildren?

    Sort them in your application. That is the only reliable way. Include doesn’t allow sorting at all and the approach showed below is not reliable because you don’t have control over EF’s internal mechanism and you cannot have your navigation property as SortedList or other collection maintaining the sort order (which is requirement to have reliably sorted relation).

    the data is flattened in one table, which means that Entity (and all others) data is duplicated for each GrandChild record

    This is valid objection. You can avoid it by turning off lazy loading and use separate queries to load data:

    context.Configuration.LazyLoadingEnabled = false;
    var parent = context.Set<Entity>().First(e => e.Name = "ABC");
    // Load relations in separate query
    context.Set<Child>()
           .Where(c => c.Parent.Name == "ABC")
           .OrderBy(c => c.Name) // You can at least try it but as mentioned above it may not work in all scenarios
           .Load();
    // Now parent.Children collection should be filled 
    

    You can follow same approach for other relation and for nested relations as well. The key is to correctly construct Where condition for children and grandchildren loading.

    It doesn’t mean that this approach will be faster that flattened table. This approach makes separate database roundtrip for every executed query so the flattened table can be faster in smaller data sets.

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

Sidebar

Related Questions

I need to retrieve data from two tables. the first is a list of
this is my code where i need to retrieve data sent in POST: @play.db.jpa.Transactional
I need to retrieve a simple page and use the data that it returns.
using jquery I need to retrieve an array from table cells, format the data
I am using a ResultSet to retrieve data from my SQL server. The code
I'm using the following code within the JCProperty class to retrieve data from a
I'm using Data::Dumper to retrieve information from a server with SOAP messaging and need
I have a situation where I need to retrieve data from a query which
Im stumbling upon a problem where i need to retrieve data from the following
Ok, I need to store/retrieve a bit from a data table of 3.268.760 bits

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.