简体   繁体   中英

query for top child items with Entity Framework (DbSet)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM