MindGame
MindGame

Reputation: 1251

Entityframework - Include nested children from parent

I'm trying to load two children from a nested parent.

dbContext.
.Where(f => f.Id == Tenant.Id)
.Include(f => f.Users
.Select(x=>x.Nicknames)
.Select(x => x.FavoriteMovies))
.SingleOrDefault();

So above I have a Tenant which has many users. Each user has two children that is one to many. Those are Nicknames and FavoriteMovies. Ex. User John has two nicknames JJ and Johnny and he has the following favorite movies Die Hard 1 and Die hard 2.

This works to get one of the children but how do I get the second one? I ultimately want to return tenant object.

  dbContext.
    .Where(f => f.Id == Tenant.Id)
    .Include(f => f.Users
    .Select(x=>x.Nicknames)
    .SingleOrDefault();

Upvotes: 0

Views: 457

Answers (1)

Dennis VW
Dennis VW

Reputation: 3177

Use the Include method to load multiple levels of related entities like this:


    dbContext.
    .Where(f => f.Id == Tenant.Id)
        .Include(t => t.Users.Select(un => un.Nicknames))
        .Include(t => t.Users.Select(uf => uf.FavoriteMovies))
    .SingleOrDefault();

Upvotes: 1

Related Questions