I have:
public class Order { public string Customer; public List<OrderLine> OrderLines; } public class OrderLine { public decimal Quantity; public decimal UnitPrice; }
My goal is to search the list of customer orders in descending order. (Amount == Adding each order Line of Quantity * UnitPrice)
Let's say we have 6 orders from 2 customers (Customer1, Customer2). Each client has 3 order orders.
I'm trying to:
var result = Database.GetAllOrders().OrderByDescending(order => order.Lines.Sum(ol=>ol.Quantity*ol.UnitPrice)) .GroupBy(order=>order.CustomerName, order=>order.Lines.Sum(ol=>ol.Quantity*ol.UnitPrice), (customerName, amount) => new {Customer=customerName, Amount=amount});
but I get something like this:
Customer2:
- Amount: 78
- Amount: 89
- Amount: 12
customer1:
- Amount: 64
- Amount: 36
- Amount: 28
while I try to find:
Customer2:
- Amount 179
customer1:
- Amount 128
Any suggestion please?
Thank you very much!
source share