entity-frameworklinqentity-framework-coremaster-detail

Entity Framework Core - Master Details Details query


I saw a lot of post showing how to perform a query within Entity Framework to retrieve master details data, like this:

IQueryable<myobj> foo = _context.Foos.Include(x => x.FooDetails).Where(x => x.Id == fooId);

But I have to manage an harder case. A master details where every detail has its own details. Something like:

Foo --> FooDetails --> FooDetailsInfo

Is this possible? If yes, how? Of course the dumb solution exists and it is use a loop. Is there a smarter way to reach this goal?

I tried to edit the line code

IQueryable<myobj> foo = _context.Foos.Include(x => x.FooDetails).Where(x => x.Id == fooId);

but I didn't write anything useful.


Solution

  • Yes, it is possible. After Include you can call ThenInclude

    var foo = _context.Foos
        .Include(x => x.FooDetails)
        .ThenInclude(fd => fd.FooDetailsInfo)
        .Where(x => x.Id == fooId);