Marianne Markwardt Marianne Markwardt - 1 year ago 179
C# Question

Entity Framework deep select

I am running into following error:

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.
Parametername: path

With the following code:

return _context.Section.Include(x => x.Leagues.Select(y => y.Games.Where(v=>v.GameStart > DateTime.Now))).ToList();

I am trying to select all sections with all leagues that have active games.
I just cannot get around this error and have not managed to find a proper solution on SF.

Thankful for any help

Regards, Marianne

Answer Source

I think this is what you want, if not you need to provide the complete data model as it relates to this question.

return _context.Section
  .Select(x => new{Section = x, Leagues = x.Leagues.Where(y => y.Games.Any(v => v.GameStart > DateTime.Now))})
  • Include is for eager loading.
  • Select is to express what you want to return and not how to filter it, very similar to T-Sql
  • Where is for filtering but it expects boolean expressions.
  • Any will evaluate the inner expression and return true if any records are evaluated as true.

Side note - keep in mind that DateTime.Now also uses the time in your evaluation expression.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download