0

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; }
}
3
  • How about using Include(TEntity) ? Commented Apr 22, 2016 at 12:07
  • could you explain? Commented Apr 22, 2016 at 12:09
  • 1
    Possible duplicate of Entity Framework - Include Multiple Levels of Properties Commented Apr 22, 2016 at 12:22

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Like I said, the subs are retrieved, I need to be able to retrieve the Categorie for the ProjectCategorie too, but I don't know how
ah ok ... add another include ... hang tight i'll update my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.