What’s wrong with this Lambda query? I want to be able to only include ProjectDocs of a certain type, there could be many types of ProjectDocs
ICollection<Project> projects = db.Projects
.Include(i => i.ProjectDoc.OfType<Cover>().Where(s => s.Status == "Active"))
.Include(i => i.ProjectDoc.OfType<Summary>().Where(s => s.Status == "Active"))
.Include(i => i.User)
.Include(i => i.ProjectTag.Select(t => t.Tag)).ToList();
I have a model ProjectDoc with the derived classes Cover, Segment and Summary. Should I just include ProjectDoc and use the discriminator column in a condition later? Some of the types could have a large number of results, others just a few.
The Error I get…
The Include path expression must refer to a navigation property defined
on the type. Use dotted paths for reference navigation properties and the
Select operator for collection navigation properties.
Parameter name: path
The Navigation Property on “Project” is ProjectDoc. There is no navigation property for the derived classes. When I tried that I got tons of extra keys.
This scenario is not supported – you can only load or not load a set of related entities but you can not apply filter expressions to load only a subset of the entities.
The API documentation for
Include()lists the different expressions that are supported and states that the method just delegates the work to an underlyingInclude()method taking a string as argument, for exampleObjectQuery.Include(). The documentation of this method and the linked page Shaping Query Results make it more or less obvious that this is not supported.