我有一個查詢,看起來像這樣的結果:選擇自IEnumerable加入一個複雜的LINQ到SQL查詢
var orderLines = from Order order in _orders
join OrderItem orderItem in dbc.OrderItems
on order.OrderId equals orderItem.OrderId
join Product product in dbc.Products
on orderItem.ProductId equals product.ProductId
join OrderItemQuantity oiq in dbc.OrderItemQuantities
on orderItem.OrderItemId equals oiq.OrderItemId
into orderItemQuantities
join ActivityQuantity aq in dbc.ActivityQuantities
on orderItem.OrderItemId equals aq.OrderItemId
into activityQuantities
orderby
order.OrderId ascending,
orderItem.OrderLineNumber ascending
select new {
Order = order,
Item = orderItem,
Product = product,
// I'd like to get these child items as IEnumerables or similar
ItemQuantities = orderItemQuantities,
ActivityQuantities = activityQuantities
};
編譯沒有問題,但導致orderItemQuantities
和activityQuantities
部分是從失蹤查詢,所以我得到一個選擇/加盟/加盟訂單/項目/產品,以及獨立的個體上OIQ/AQ的每個條目選擇:
SELECT (...) FROM [ORDERS] AS t0
INNER JOIN [ORDER_ITEMS] AS t1 ON t0.ORDER_ID = t1.ORDER_ID
INNER JOIN [PRODUCTS] AS t2 ON t1.PRODUCT_ID = t2.PRODUCT_ID
ORDER BY (...)
然後,爲每個行的,它執行這些查詢:
SELECT (...) FROM [ACTIVITY_QUANTITY] as t0
WHERE t0.ORDER_ITEM_ID = @p0
SELECT (...) FROM [ORDER_ITEM_QUANTITY] as t0
WHERE t0.ORDER_ITEM_ID = @p0
由於我有數以萬計的行,這導致了一個荒謬的數量的查詢。
有沒有辦法凝聚了OrderItem
的OrderItemQuantity
和ActivityQuantity
條目爲IEnumerable
(或類似)的方式,使他們很容易從在選擇使用的匿名類型訪問?
這是不是簡單地將仍然執行單個查詢每次'遞延枚舉創建一個IList的'' IEnumerator.MoveNext'被調用? **更新:**是的,證實它只是簡單地將底層枚舉器轉換爲'IList ',並且查詢仍然是分開的。 –
Polynomial
@Polynomial一個恥辱。然後刪除這個答案。 EF爲你列出清單:( – AakashM
不要刪除答案,它可能會幫助其他人排除這種可能性。 – Polynomial