I've got a query that looks like this:
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
};
This compiles fine, but results in the orderItemQuantities
and activityQuantities
parts being missing from the query, so I get a single select/join/join for order/items/products, and separate individual selects on OIQ/AQ for each entry:
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 (...)
Then, for each of those rows, it performs these queries:
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
Since I've got tens of thousands of rows, this results in a ridiculous number of queries.
Is there a way to coalesce the OrderItemQuantity
and ActivityQuantity
entries for the OrderItem
into an IEnumerable
(or similar) in a way that makes them easily accessible from the anonymous type used in the select?