Michiel
Michiel

Reputation: 113

include multiple objects inside an object [Entity framework]

I'm trying to retrieve both the list of ProjectCategories (subcategories) and the Categorie of a ProjectCategorie, the sub lists are retrieved. But I have no idea how to get the Categorie.

Project project = ctx.Projecten.Include(p => p.ProjectCategories.Select(s => s.Sub.Select(su => su.Sub))).ToList().Find(p => p.ProjectId == projectId);

These are the domain classes, as you can see I have a Categorie inside my ProjectCategorie:

public class ProjectCategorie
{
    public int ProjectCategorieId { get; set; }
    public double MinBedrag { get; set; }
    public double MaxBedrag { get; set; }
    public bool Aanpasbaar { get; set; }
    public bool AutoAanpasbaar { get; set; }

    public ProjectCategorie Super { get; set; }
    public List<ProjectCategorie> Sub { get; set; }
    public Project Project { get; set; }
    public Categorie Categorie { get; set; }
}

public class Categorie : BegrotingsPost
{

    public int CategorieId { get; set; }
    public string Beschrijving { get; set; }
    public double MinBedrag { get; set; }
    public double MaxBedrag { get; set; }

    public Begroting Begroting { get; set; }
    public BegrotingsPost Super { get; set; }
}

Upvotes: 0

Views: 2064

Answers (1)

War
War

Reputation: 8618

Ok update (mis read the question) ...

Project project = ctx.Projecten
   .Include(p => p.ProjectCategories.Select(s => s.Sub.Select(su => su.Sub)))
   .Include(p => p.ProjectCategories.Select(s => s.Categorie))
   .Find(p => p.ProjectId == projectId)
   .ToList();

I would probably do the find in the db too (might introduce a logic error else). Just moving the ToList() to the last line of the query will resolve that.

Upvotes: 9

Related Questions