【问题标题】:Where on child's parent object孩子的父对象在哪里
【发布时间】:2019-07-22 17:18:21
【问题描述】:

我正在尝试获取孩子的 parentId 等于页面 id 的所有对象。

这就是我想要做的:

public IEnumerable<Route> GetAll(int locationId)
{
    _db.Configuration.ProxyCreationEnabled = false;
    return _db.Routes.Include(o => o.ConnectionPointRoutes.Select(s => s.Segment))
            .Include(o => o.ConnectionPointRoutes.Select(c => c.ConnectionPoint)
            .Where( c => c.Location.LocationId == locationId)).ToList();
}

但我不断收到此错误:

EntityFramework.dll 中出现“System.ArgumentException”类型的异常,但未在用户代码中处理

附加信息:包含路径表达式必须引用 在类型上定义的导航属性。使用虚线路径 参考导航属性和用于集合的 Select 运算符 导航属性。

有什么想法吗?

【问题讨论】:

  • 看起来 c.Location 是一个列表对象,所以你需要 c.Location.Select( x => ......)。您是否有像曾祖父 ID、祖父 ID、父 ID、子 ID 这样的多代表?如果是这样,您可能需要使用递归算法来获取所有级别。 Linq 仅适用于单代列表。
  • 我认为Include 内部不支持Where
  • 我找到了解决方法,有兴趣可以查看答案:)

标签: c# entity-framework linq linq-to-sql


【解决方案1】:

我最终这样做了:

    public IEnumerable<Route> GetAll(int locationId)
    {
        return _db.Routes
            .Include(o => o.ConnectionPointRoutes.Select(s => s.Segment))
            .Include(o => o.ConnectionPointRoutes.Select(c => c.ConnectionPoint))
            .Where(c => c.ConnectionPointRoutes.Select(s => s.ConnectionPoint.Location)
            .FirstOrDefault(q => q.LocationId == locationId).LocationId == locationId)
            .ToList();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多