2
var result = ParentTable.Set
.Include("Children")
.Include("Children.GrandChildren")
.SelectMany(p=>p.Children)
.Where(c=>c.GrandChildren.Any(whatever))

will return null, even though

var result = ParentTable.Set
.Include("Children")
.Include("Children.GrandChildren")

has the fully populated data in it.

Its needed to do

 var result = ParentTable.Set
.SelectMany(p=>p.Children)
.Include("GrandChildren")
.Where(c=>c.GrandChildren.Any(whatever))

This example is a bit contrived, but I ran into this problem as part of a larger more complicated query where a subselect projection was using SelectMany to populate a count into a particular property, and other data was being projected into other properties from outside the selectMany, and those properties were populating, but the selectmany count was not. It was very confusing how different parts of the query had different parts of the same data lazy loaded!

Jason Coyne
  • 6,179
  • 7
  • 35
  • 64
  • Try enumerating your query before doing the SelectMany. Not sure, but I've had similar issues in the past the have been solved by enumerating. – Avrohom Yisroel Sep 27 '16 at 22:34
  • It was already enumerated actually. It does not make a difference. Part of the "whatever" in my "Any" relies on some properties in the model that can't be processed on the server. – Jason Coyne Sep 28 '16 at 15:58

1 Answers1

1

You should be using ThenInclude to get the children. I would rewrite your query as:

var result = ParentTable.Set
.Include(p => p.Children)
.ThenInclude(c => c.GrandChildren.Where(gc => gc.whatver))
.SelectMany(p => p.Children)
Rafael Herscovici
  • 14,724
  • 15
  • 61
  • 91