Could a smart guy help me write a LINQ query to get the best selling products, where I have these 2 tables/classes in the Entity Framework model:
Orders (OrderedItems)
OrderedItem (Product, Qty)
eg. (this obviously doesn't work)
Orders.OrderByDescending(o => Sum(o.OrderedItems.Qty))
.Select(o => o.OrderedItems.Product)
You don't really need the Orders table at all. Assuming every ordered item has a corresponding order, you should be able to do this.
var query =
(from item in OrderedItem
group item.Qty by item.Product into g
orderby g.Sum() descending
select g.Key).Take(5); // get the top 5 orders
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With