I have a list of Order objects. Each Order object has a list of OrderItems. The OrderItem object has a ProductId, and quantity property.
Given a list of Order objects (ex: var orders = _dbContext.Orders;) how can I find my top selling ProductIds (based on total quantities over all the orders in my list)?
I guess I'm looking for a LINQ statement.
You can use
orders.SelectMany(o=> o.OrderItems).OrderByDescending(oi=> oi.quantity).Take(5)
I would be careful at the generated sql though, sometimes it gets ugly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.