I am trying to query an entity with multiple levels of collections, and multiple collections at a single level. I'm using Include()
and ThenInclude()
, but not having much success. The examples I find don't have multiple collections on the same level and I haven't had any luck applying the technique to my use case.
This is a simplified illustration of my entities. Those with the [] are collections:
Home
Areas[]
Area
Name
Categories[]
Name
Recommendations[]
Subcategories[]
Name
Recommendations[]
Area
Name
Categories[]
Name
Recommendations[]
Subcategories[]
Name
Recommendations[]
I've gotten this far:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(c => c.Subcategories)
.ThenInclude(s => s.Recommendations)
However, this misses the Categories[].Recommendations[]
collection. It's because there are two collections at the same level (Recommendations[]
and Subcategories[]
).
Any suggestions for a way to structure this query so I can get the desired result?
Thanks.
You have to call Include
for each level:
result = Home
.Include(x => x.Areas)
.ThenInclude(a => a.Categories)
.ThenInclude(c => c.Subcategories)
.ThenInclude(s => s.Recommendations)
.Include(x => x.Areas)
.ThenInclude(a => a.Recommendations)