0

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

  • I think this is what you want. `return _context.Section.Where(x => x.Leagues.Any(y => y.Games.Any(v => v.GameStart > DateTime.Now))).ToList();` the `Include` is for eager loading. The `Select` is to express what you want to return and not how to filter it which is what `Where` is for. `Where` expects boolean expressions which is where `Any` comes in to play. – Igor Oct 17 '16 at 20:04
  • `Include` is not for filtering. It implements so called "eager loading" ([reference](https://msdn.microsoft.com/en-us/data/jj574232.aspx)). To filter the result, use a normal `Where` outside the `Include`. – Ivan Stoev Oct 17 '16 at 20:09
  • Hello Igor. Almost. I need ALL sections. I need the leagues with active games. Sorry for bad explanation. – Marianne Markwardt Oct 17 '16 at 20:09
  • @MarianneMarkwardt - updated the answer below. – Igor Oct 17 '16 at 20:13

1 Answers1

1

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))})
  .ToList();
  • 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.

Igor
  • 60,821
  • 10
  • 100
  • 175