Here is a simple LINQ query (based on NorthWind) that returns a list of clients. Each customer contains a list of orders.
from c in Customers
join o in Orders on c.CustomerID equals o.CustomerID into CO
select new {c, CO}
This works great, and the generated SQL is fine too. Now I want to take it one step further. I want each Order object to contain an OrderDetails list. I am using the following query:
from c in Customers
join od in (
from o in Orders
join od in OrderDetails on o.OrderID equals od.OrderID into OD
select new { o.CustomerID, o, OD }
)
on c.CustomerID equals od.CustomerID into COD
select new { c, COD }
This query works, but generates horrible SQL. Each client is issued a separate request. When you look at the lambda code, we have:
Customers
.GroupJoin (
Orders
.GroupJoin (
OrderDetails,
o => o.OrderID,
od => od.OrderID,
(o, OD) =>
new
{
CustomerID = o.CustomerID,
o = o,
OD = OD
}
),
c => c.CustomerID,
od => od.CustomerID,
(c, COD) =>
new
{
c = c,
COD = COD
}
)
Nested GroupJoins appear to be the cause of multiple SQL stataments. However, I tried various combinations without success. Any ideas?
EDIT:
, , . , OrderDetail Order, , , Customer. , Order OrderDetail . . OrderDetails. , , .