实体框架深度选择

本文关键字:选择 深度 框架 实体 | 更新日期: 2023-09-27 18:13:22

我遇到以下错误:

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

,代码如下:

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

我正在尝试选择所有有活动游戏的联赛的所有区域。我只是无法绕过这个错误,也没有设法在SF上找到适当的解决方案。

感谢您的帮助

问候,玛丽安

实体框架深度选择

我认为这就是你想要的,如果不是,你需要提供完整的数据模型,因为它与这个问题有关。

return _context.Section
  .Select(x => new{Section = x, Leagues = x.Leagues.Where(y => y.Games.Any(v => v.GameStart > DateTime.Now))})
  .ToList();
  • Include为急装。
  • Select是表达你想要返回什么,而不是如何过滤它,非常类似于T-Sql
  • Where用于过滤,但需要boolean表达式。
  • Any将计算内部表达式,如果任何记录被计算为true,则返回true

旁注-请记住,DateTime.Now也在计算表达式中使用时间。