Suppose I have an Order entity and an OrderItem entity, and that I'm trying to get the details (including items) of several orders at once.
I could get the Order and the OrderItem details together using .Include
like so:
var orders = db.Orders
.Include(o => o.OrderItem)
.Where(o => orderIds.Contains(o.Id));
But, suppose that I can't use .Include
because it doesn't work.
Instead, I could fetch all of the Orders, and then fetch all of the OrderItems, like this:
var orders = db.Orders.Where(o => orderIds.Contains(o.Id));
var orderItems = db.OrderItems.Where(i => orderIds.Contains(i.OrderId));
This will give me all the Orders and (separately) all the relevant OrderItems. But how do I "attach" the OrderItem
objects to the relevant Order
objects so that the navigation properties work correctly?
(In other words, how do I ensure that order.OrderItems
and orderItem.Order
work without causing EF to go fetch the data from the database rather than using what I've already retrieved).