Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ query to select best sellers

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)
like image 743
Aximili Avatar asked Dec 15 '22 17:12

Aximili


1 Answers

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
like image 98
Jeff Mercado Avatar answered Dec 18 '22 08:12

Jeff Mercado