I have the following data structure (just a exemplary visual representation):
list<customer>
{
customer
{
name
orders[]
{
order
{
quantity
product
{
price
}
}
order
{
...
}
order
{
...
}
}
customer
{
...
}
customer
{
...
}
}
And I want to acquire the name of each Customer and the total sum of his orders (qty*product price). Now I have the following:
var sumEachCust = from c in customers
let sum = 0.0m
select new
{
c.Name,
sum =+ (from o in c.Orders
select o.Product.Price * o.Quantity).FirstOrDefault()
};
If the Customer object has more than one Order entry in its Orders[], then I want the sum of all the entries inside. But with this query I get the sum just for the first order.
If I make it this way:
var sumEachCust = from c in customers
let sum = 0.0m
from o in c.Orders
select new
{
c.Name,
sum =+ o.Product.Price * o.Quantity
};
then I will get the name as many times as there are order entities in the Orders[] property of the Customer object, and I don't want that.
Simply - just once name and sum of all orders.