Unable to include in depth
Description
Code blow should include maxDepth layers of self containing objects with related properties but instead it just include for 2 levels and it sounds bug to be, because finally i used Include(string) method to include all inner objects and it worked perfectly, also I've tried multiple recursive kinds of including and none of them worked as they should.
Include your code
public static IQueryable<Foo> IncludeInnerFoos(
this IQueryable<Foo> Foo,
int maxDepth)
{
IQueryable<Foo> result = Foo;
for (int j = 1; j < maxDepth; j++)
{
result = Foo
.Include(i => i.LowerLevelFoos)
.ThenInclude(i => i.Bar)
.ThenInclude(i => i.BarZ)
.Include(i => i.LowerLevelFoos)
.ThenInclude(i => i.Bar)
.ThenInclude(i => i.BarX)
.ThenInclude(i => i.BarXY);
}
return result;
}
Include provider and version information
EF Core version: 8.0.2 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 8.0 Operating system: Windows IDE: Rider
@danirzv Include always starts from the root. ThenInclude is the operation that builds on top of the previous one, so you need to recurse on these, not the entire chain. Also, in the code provided you never update Foo so the loop keeps assigning the same expression to result over and over.
In general, for dynamic include generation I'd recommend using string based Includes