I'm new to C# and have problem I would usually solve with a subquery, but unsure how to go about it here.
I have a table called Orders
that has many OrderLines
(the foreign key is on the OrderLines
).
The OrderLines
have a lot of data, but I'm only interested in the sum of all OrderLines.value
for each Order
.
In pseudocode I want something like
DbSet.(o:order => o)
.Include(o => Sum(o.OrderLines.value)
What is the idiomatic way to do something like this?
CodePudding user response:
Something like this:
var ordersWithLineTotal = db.Set<Order>()
.Select(o => new { Order = o, LineTotal = o.OrderLines.Sum(l => l.LineTotal) })
.ToList();