I have this model of a project:
public class Project
{
public int Id { get; set; }
public int? ParentId { get; set; }
public Member ProjectOwner { get; set; }
public List<Project> ChildProjects { get; set; }
// ... more properties
}
I want to load all child projects and their ProjectOwner
for all projects. In my setup, there is no set limit for how long a tail of descendants can be until we get to the "leaf".
At the moment, I'm doing this for the Index
-view (yes, it's terrible!):
List<Project> projects = await db.Projects
.Where(p => p.ParentId == null)
.Include(o => o.ProjectOwner)
.Include(c => c.ChildProjects)
.ThenInclude(o => o.ProjectOwner)
.Include(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(o => o.ProjectOwner)
.Include(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ProjectOwner)
.Include(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ProjectOwner)
.Include(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ChildProjects)
.ThenInclude(c => c.ProjectOwner)
// ... and so on ... (!)
.ToListAsync();
How can I load everything in a more effective and dynamic way? The above code is limitied to a fixed number of levels. I can't have that.
I asked a similar question a few days ago, but was not able to make anything out of the replies.