2011-08-31 72 views
2

我使用實體框架4.0我正在與下面的查詢問題:和LINQ查詢與包括不似乎返回正確的結果在EF 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沒有問題,不知道我這次做錯了什麼。

+0

你是如何配置在users'和'orders_assigned'之間'你的模型之間的關係?似乎那裏可能有問題。 – newdayrising

+0

該關係在EF中由foriegn鍵定義 - 我使用包含返回正確結果的其他linq查詢,由於某些原因,這不是tho – 99823

回答

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.