1

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.

1 Answer 1

8
from c in customers
select new
{
     Name = c.name,
     OrderTotal = c.orders.Sum(o => o.product.price * o.quantity)
}

If you really want to use let then here is how you would do that for this scenario

from c in customers
let sum = c.orders.Sum(o => o.product.price * o.quantity)
select new
{
    Name = c.name,
    OrderTotal = sum
}
Sign up to request clarification or add additional context in comments.

4 Comments

Works and is a really clean and simple solution! Thanks a lot! Nevertheless, could you give me an example using let and temporary variable in the query, since I was going that way and would like to see how to correctly use a nested select statement in this case?
@Milkncookiez see update, although you really don't need to use let at all here.
sorry, I wanted to ask about an example with a nested select, because for example, if I have to sum the orders of all customers together into one total sum (instead of summing the orders of each customer separately), then how would I do that? (given that I am not completely sure how to use the iteration logic of nested query).
@Milkncookiez to return the sum of all the records then you can simply do let sum = customers.Sum(x => x.orders.Sum(o => o.product.price * o.quantity)) - bare in mind this will result in a separate query though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.