How to make linq master detail query for 0..n relationship?
Posted
by JK
on Stack Overflow
See other posts from Stack Overflow
or by JK
Published on 2010-05-18T03:53:22Z
Indexed on
2010/05/18
5:00 UTC
Read the original article
Hit count: 348
Given a classic DB structure of Orders has zero or more OrderLines and OrderLine has exactly one Product, how do I write a linq query to express this?
The output would be
OrderNumber - OrderLine - Product Name
Order-1 null null // (this order has no lines)
Order-2 1 Red widget
I tried this query but is not getting the orders with no lines
var model = (from po in Orders
from line in po.OrderLines
select new
{
OrderNumber = po.Id,
OrderLine = line.LineNumber,
ProductName = line.Product.ProductDescription,
}
)
I think that the 2nd from is limiting the query to only those that have OrderLines, but I dont know another way to express it. LINQ is very non-obvious if you ask me!
© Stack Overflow or respective owner