Understanding Include().ThenInclude() in EF core - At levels beyond the first

asp.net-core-mvc entity-framework-core linq


I know that Include then include should be used when an entity span multiple tables using what called eager loading querying; but I don't fully understand is the difference between include and theninclude

my guess is Include() is to access the first layer only and ThenInclude() is to access the next layer ?? but if that's right how can we access two collection in third layer with the following pattern

a -> a.b->a.b.a

     a.c-> a.c.a

and here's two scenarios and my attempt to solve it, can someone also check if i'm doing it right

in a scenario where I have Order class that contain

public ICollection<basket> items { set; get; }
public Address address { set; get; }

Basket include

    public Product product { get; set; }

2- Is this right way to query it?

 public IEnumerable<Order> Orders

     get {  return context.Orders.Include(o => o.address).Include(o => o.items).ThenInclude(p => p.product);


and if I have recursive association

section/topic/post/ where section has subsection section definition include so here a subsection of type section

        public ICollection<Thread> threads { set; get; }

        public ICollection<Section> subSections { set; get; }

and Thread entity include

        public ICollection<Portfolio_Domain.Entities.Post> Posts { set; get; }

3- is this a correct way to use it?

    public IEnumerable<Section> Sections
                get {  return context.Sections.Include(s=>s.SubSections.Where(ss=>ss.subSectionID==s.ID)).Include(t=>t.Threads).ThenInclude(p=> p.Posts);

Update: I found my solution to 2- and 3- but not 1- yet

for #1 Iwant to achieve something like context.a->include(b).theninclude(b.a, b.b).include(c).theninclude(c.a) more importantly a code that work for any level, a code that can work for the next level too like following context.a->include(b).theninclude(b.b).theninclude(b.b.a,b.b.b)

I'm not struggling with including multiple data from the first level, the problem i'm struggling with is to including multiple datatypes from third or fourth level I hope this explain it and sorry for my English but this is the best I can explain

9/12/2017 2:06:13 PM

Accepted Answer

Here's the answer for #2 and #3 as I implemented it so far and it works as expected


public IEnumerable<Order> Orders=> context.Orders.Include(o => o.address).Include(o => o.items).ThenInclude(p => p.product);


Note that I removed some stuff from the query to make it match the question posted here

public IEnumerable<Section> Sections => context.Sections.Include(s => s.SubSections).ThenInclude(sxs=>sxs.SubSections).Include(t => t.Threads).ThenInclude(p => p.Posts);

So to explain my answer for #3 it's very simple;

Just keep adding .ThenInclude(subsections) for each level you want to get the result for.

Cons: You need to know how deep your query will be so if you have Chairman-> Vicechairman-> S.E.V.P->manager->it>intern that's one include and 5 theninclude for the associations if you want to return the intern data

9/12/2017 2:06:55 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow