我有以下表格
[Order]
[Product]
ProductId
[OrderDetails]
OrderId
ProductId
Quantity
我必须使用 Order 对象来获取包含按销售额分组的总产品销售额的列表
public class ProductSales
{
public decimal Total { get; set; }
public Product Product { get; set; }
}
给定一个订单对象列表,我如何选择其产品详细信息,然后按产品对销售进行分组?
我当前的代码如下所示:
return Orders
.Select(ordeDetail => new {
Detail = ordeDetail.OrderDetails
.GroupBy(w => new {
Detail = w,
Product = w.Product})
.Select (group => new ProductSales {
Product = group.Key.Product,
Quantity = group.Sum(r => r.Quantity)
})
.ToList();