Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sales grouped by month then by product

Tags:

linq

I want to get the list of sales for each month ordered by product.

Currently I have the following linq query:

        var query = storeDB.OrderDetails
            .Select(od => od)
            .GroupBy(r => r.Product)
            .Select(group => new ProductSalesModel {
                Product = group.Key,
                Sales = group.Sum(s => s.Quantity),
                Amount = group.Sum(s => s.Quantity * s.UnitPrice)
            })
            .OrderByDescending(x => x.Amount);

How should I do to also group the list by month getting the sales date from my Order table ? Order.CreationDate?

like image 327
Marc Avatar asked Dec 06 '25 19:12

Marc


1 Answers

I don't know what Linq provider you are trying to use but in plain Linq this should work:

 .GroupBy(r => new {r.Product, r.CreationDate})

and then when you select:

.Select(group => new ProductSalesModel {
                Product = group.Key.Product,
                CreationDate = group.Key.CreationDate,
                Sales = group.Sum(s => s.Quantity),
                Amount = group.Sum(s => s.Quantity * s.UnitPrice)
            })
like image 196
Variant Avatar answered Dec 08 '25 21:12

Variant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!