EF code-first: How to load related data (parent-child-grandchild)?

12,304

EF 4.1 feature and syntax:

var entity = context.Parents
    .Include(p => p.Children.Select(c => c.GrandChildren))
    .FirstOrDefault(p => p.Id == 1); // or whatever condition
Share:
12,304
amiry jd
Author by

amiry jd

My other SO-Profile: https://stackoverflow.com/users/974276/j-amiry I am a hard-working and innovative developer with near 18 years of experience mostly based on .NET (Framework & Core) technology stack. For the last few years, alongside developing some awesome applications, I was focused on some non-coding tasks such as building up development teams, team leading, tech leading, problem solving, consulting, architecting, reviewing other's code, and mentoring. As a self-motivated fast-learner experienced code-guy, who has a proactive personality to step up to new challenges, I think I'm the man who can get the job done.

Updated on June 14, 2022

Comments

  • amiry jd
    amiry jd over 1 year

    I have this entity:

    public class DynamicPage {
    
        public int PageId { get; set; }
    
        public int Order { get; set; }
    
        public string MenuText { get; set; }
    
        public string MenuHover { get; set; }
    
        public int? ParentId { get; set; }
    
        public virtual DynamicPage Parent { get; set; }
    
        public virtual ICollection<DynamicPage> Children { get; set; }
    }
    

    This entity may have 3 level: Parent -> Child -> Grandchild. How can I load the Parent (level 1) whit all associated children (level 2) and for each child, associated grandchild (level 3) if any? Thanks to help.

  • amiry jd
    amiry jd about 12 years
    What are u talking about? Plz read the Q again: How can I load the Parent (level 1) whit all associated children (level 2) and for each child, associated grandchild (level 3) if any? I know how to map entities, and know how to load them in 2-level relationship Parent-Child. But I can't load a 3-level association. Just this!
  • devuxer
    devuxer about 12 years
    What do you mean by "load"? Do you mean select?
  • amiry jd
    amiry jd about 12 years
    Load from database, fetch from db, select. Like this: entity.DynamicPages.Where(d => d.ParentId == null).Include(d => d.Children).ToList(); I know this, but I want to load d.Children.Children that I can't ):
  • devuxer
    devuxer about 12 years
    Updated my answer. Hopefully this is what you were looking for.
  • amiry jd
    amiry jd about 12 years
    Thanks, I will test it and put the result here. Regards
  • devuxer
    devuxer about 12 years
    Well in that case, you just need to union them together. I updated my answer again.
  • amiry jd
    amiry jd about 12 years
    There is not a property named GrandChildren. Do you mean this code: Include(p => p.Children.Select(c => c.Children)) ?
  • Slauma
    Slauma about 12 years
    @Javad_Amiry: Yes. Actually, what I wrote as GrandChildren can be any navigation property - collection or reference. The pattern Include(x => x.SomeCollection.Select(c => c.SomeNavigationProperty)) just causes an Include for the next level. You can repeat that ad infinitum: Select on collections, simple dotted paths on references.
  • amiry jd
    amiry jd about 12 years
    OK, this works correctly :D thanks a lot. In continues I need to order children (and so grand children) when fetching them. I'll edit your answer and put my first code, and final code created by your guidance to explain my issue. Please help me to complete the solution. Special thanks to pay attention to question. Regards
  • Slauma
    Slauma about 12 years
    @Javad_Amiry: Better make a new question about your sorting problem because 1) more people will see a new question than an edit in my answer, and 2) I'm offline now :)
  • amiry jd
    amiry jd about 12 years
    OK, before I see your comment, I post my edit. So sorry. I'll put it to a new question just now. Please rollback my edit, SOF don't allow me to do it by myself. Thanks again.
  • amiry jd
    amiry jd about 12 years
    Thank you to pay attention on my question, but my answer is not this, and because of I can't speak English good, I can't explain my purpose. Regards
  • devuxer
    devuxer about 12 years
    Now that I see Slauma's answer, I get what you were looking for. And I also learned something, so no problem.
  • amiry jd
    amiry jd about 12 years
    I moved the part 2 of question (ordering children and grandchildren) here: stackoverflow.com/questions/7522784/… I'll be thankful if see the new question and suggest me if have any idea. Good lock and best regards.
  • Chris Moschini
    Chris Moschini over 11 years
    Has this changed in EF4.3? The only version of .Include() I can see in 4.3 takes a string instead of a typesafe navigator like x => x.Children - a step backwards. This is reflected in the documentation here: asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4 Seems strange to go backwards like that in a newer version of the framework...
  • Slauma
    Slauma over 11 years
    @ChrisMoschini: No, it hasn't changed. The expression overload of Include is still there. Make sure that you have included using System.Data.Entity; in your source file.