tags:

views:

75

answers:

1

I have a order list and I want to generate and rank the product with its total sales and quantity. With @tvanfosson's help, I can bring the grouped product detail with the following code, but how can I calculate and add up the total sales and quantity into each productListResult's object?

Can anyone help me with this?

Many thanks.

        var productListResult = orderProductVariantListResult
                .Select(pv => pv.Product)
                .GroupBy(p => p)
                .Select(g => new 
                {
                    Product = g.Key,
                    TotalOrderCount = g.Count()
                })
                .OrderByDescending(x => x.TotalOrderCount).ToList();
+2  A: 

Something like this?

var productListResult = orderProductVariantListResult
        .GroupBy(pv => pv.Product)
        .Select(g => new 
        {
            Product = g.Key,
            TotalOrderCount = g.Count(),
            TotalSales = g.Sum(pv => pv.Sales),
            TotalQuantity = g.Sum(pv => pv.Quantity),
        })
        .OrderByDescending(x => x.TotalOrderCount).ToList();
Mark Byers
Hi Mark, I tried this and it did not get compiled, because the Sales and Quantity are not the properties of Product. Any other ideas?
Daoming Yang
@Daoming: You may have mistyped. Notice that I removed a line. Try copy and pasting my exact code - and don't just copy the last few lines, copy the entire block. It compiles fine for me, and the error you get will happen if you forget to remove the line I removed, so I suspect that you haven't copied it correctly. If you still think it doesn't work, post the *exact* error message. Again: please use copy and paste. (PS: Ctrl-C = copy and Ctrl-V = paste).
Mark Byers
@Mark, You are right. I should not select product and then group them. I understand it now. The big thanks for you. Hmmm.... I would like to buy you a drink.
Daoming Yang