【问题标题】:Entity Framework deep select [duplicate]实体框架深度选择[重复]
【发布时间】:2016-10-17 20:02:43
【问题描述】:

我遇到以下错误:

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.Where(x => x.Leagues.Any(y => y.Games.Any(v => v.GameStart > DateTime.Now))).ToList(); Include 用于预加载。 Select 是表达您想要返回的内容,而不是如何过滤它,这是 Where 的用途。 Where 需要布尔表达式,这是 Any 发挥作用的地方。
  • Include 不用于过滤。它实现了所谓的“急切加载”(reference)。要过滤结果,请在Include 之外使用普通的Where
  • 你好,伊戈尔。几乎。我需要所有部分。我需要有活跃比赛的联赛。抱歉解释不好。
  • @MarianneMarkwardt - 更新了下面的答案。

标签: c# entity-framework linq


【解决方案1】:

我认为这就是您想要的,如果不是,您需要提供与此问题相关的完整数据模型。

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 也会在您的评估表达式中使用时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多