【问题标题】:Linq Query with Include Doesn't Appear to be Return Proper Results in EF 4.0在 EF 4.0 中,带有 Include 的 Linq 查询似乎不会返回正确的结果
【发布时间】:2011-08-31 20:22:16
【问题描述】:

我正在使用 Entity Framework 4.0,但遇到以下查询问题:

IQueryable<user> users = 
    from u in Entities.users.
        Include("orders_assigned").Include("orders_assigned.order_line_items")
    from o in u.orders_assigned
    where 
        o.status.Equals((int)OrderStatus.ReadyForInvestigation) &&
        o.assigned_to_user_id != 0
    from oli in o.order_line_items
    where 
        oli.line_item_type.Equals("service") ||
        oli.line_item_type.Equals("package_service")
    select u;

我正在尝试返回一个用户列表,其中包含他们的订单子列表,其中包含订单行项目的子列表(类似于 user->orders->order_line_items),如上面的包含所示 - 但是无论何时我打电话给ToTraceString 这个查询它显示只返回一个用户列表。

我之前用过Include 没有问题,不知道这次我做错了什么。

【问题讨论】:

  • 您如何在模型中配置usersorders_assigned 之间的关系?那里似乎有问题。
  • 关系是在 EF 中由外键定义的——我已经使用 include 执行了其他 linq 查询,返回了正确的结果,由于某种原因,这不是 tho

标签: c# linq entity-framework-4 linq-to-entities


【解决方案1】:

试试:

IQueryable<user> users = ((ObjectQuery<user>)
    from u in Entities.users
    from o in u.orders_assigned
    where 
        o.status.Equals((int)OrderStatus.ReadyForInvestigation) &&
        o.assigned_to_user_id != 0
    from oli in o.order_line_items
    where 
        oli.line_item_type.Equals("service") ||
        oli.line_item_type.Equals("package_service")
    select u).Include("orders_assigned").Include("orders_assigned.order_line_items");

Explanation here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2015-02-10
    相关资源
    最近更新 更多