I’m looking for help with a query.
I have this classic Categories -> Products -> OrderDetails <- Orders table layout. (No Customers table though)
What im missing is Categories.Name and Products.Name in that OrderItems list. Can you please help?
from o in Orders
join od in OrderDetails on o.Id equals od.OrderId into oi
select new {
o.Id,
o.CustomerName,
OrderItems = oi
}
First of all, you should have proper foreign keys in place in your database.
If so, you can simply reference oi.Product.name and oi.Product.Category.Name
Howver, they might not be loaded due to the lazy (deferred) loading in Linq2Sql and/or your datacontext already being disposed by the time you reference them.
In these cases, you can use the LoadOptions on the datacontext to eager load product and category together with orderitems and you are all set.