EF Explicitly loading collection within collection

Viewed 1041

Simple question but from the Entity Framework MSDN examples and other searches I can't see the correct way to do this.

I can explicitly load an EF collection like so:

db.Entry(project).Collection(p => p.Reports).Load();

But if each report has a list of pages within them that need to load, how do I do it? I figured it was something like:

db.Entry(project.Reports).Collection(r => r.Pages).Load();

But this won't work, because 'r' is an ICollection of Reports. What is the correct way to load collections within collections?

1 Answers

You can use this syntax:

db.Entry(project).Collection(p => p.Reports)
  .Query()
  .Include(r => r.Pages).Load();

As per MSDN, Query ...

Returns the query that would be used to load this collection from the database. The returned query can be modified

... which means that also Include can be applied to it.

Related