2

I want to retrieve a DB set with as few obsolete-data as possible, so I wrote the following include-statement

using (var context = this.Container.Resolve<IDbContextScope>()
{
    var databaseRequirements = context.Requirements
      .Include(x => x.RequirementVersions.MaxBy(y => y.Version))
      .Include(x => x.RequirementVersions.Select(y => y.RequirementDetails));
}

so later I can use my foreach;

foreach (MyRequirement requirement in databaseRequirements)
{
    var databaseRequirementDetails = requirement.RequirementVersion.FirstOrDefault()?.RequirementDetails;
    // Some more code that uses the var...
}

The database that is used here has 3 tables, Requirements, RequirementVersions, RequirementDetails. 1 Requirement has many RequirementVersions and 1 RequirementDetails has many RequirementVersions.

I initially ran with the MaxBy() from the .Include()-statement in place of the .FirstOrDefault(), but that'll retrieve all the old versions of a requirement, which I don't need in this particular method (and depending on the amount of requirements/requirementversions may slow the whole method down tremendously).

On to the actual problem; When I run this, 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.

I couldn't find anything on how to write this statement in a way it's accepted and does what I intend to do. It's an optimization-effort, so as long as it runs quicker than the alternative I'll settle.

Edit: It is not a duplicate of the linked article because the linked article's accepted answer returns a single object, whereas I need a list of objects who's lists contain a single object.

Community
  • 1
  • 1
Ciphra
  • 269
  • 2
  • 17
  • 1
    Linq To SQL and Entity Framework does not support filtered includes. – Salah Akbari Oct 17 '16 at 09:51
  • The number of open/closed parenthesis do no match in the include statement. Until you fix that error the code will not compile properly. – jdweng Oct 17 '16 at 09:52
  • @jdweng my bad, I copied the piece of code while I was editing it. I've updated the mainpost to match open/close. Original problem was not a compile error though, but a runtime error. – Ciphra Oct 17 '16 at 09:54
  • https://entityframework.codeplex.com/workitem/47 – Mat Oct 17 '16 at 10:00

0 Answers0