EF code-first: How to load related data (parent-child-grandchild)?
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
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, 2022Comments
-
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 about 12 yearsWhat 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 about 12 yearsWhat do you mean by "load"? Do you mean select?
-
amiry jd about 12 yearsLoad 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 loadd.Children.Children
that I can't ): -
devuxer about 12 yearsUpdated my answer. Hopefully this is what you were looking for.
-
amiry jd about 12 yearsThanks, I will test it and put the result here. Regards
-
devuxer about 12 yearsWell in that case, you just need to union them together. I updated my answer again.
-
amiry jd about 12 yearsThere is not a property named
GrandChildren
. Do you mean this code:Include(p => p.Children.Select(c => c.Children))
? -
Slauma about 12 years@Javad_Amiry: Yes. Actually, what I wrote as
GrandChildren
can be any navigation property - collection or reference. The patternInclude(x => x.SomeCollection.Select(c => c.SomeNavigationProperty))
just causes anInclude
for the next level. You can repeat that ad infinitum:Select
on collections, simple dotted paths on references. -
amiry jd about 12 yearsOK, 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 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 about 12 yearsOK, 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 about 12 yearsThank 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 about 12 yearsNow that I see Slauma's answer, I get what you were looking for. And I also learned something, so no problem.
-
amiry jd about 12 yearsI 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 over 11 yearsHas 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 over 11 years@ChrisMoschini: No, it hasn't changed. The expression overload of
Include
is still there. Make sure that you have includedusing System.Data.Entity;
in your source file.