0

我有以下表格

[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();
4

1 回答 1

0

您可以按产品分组(按其 ID 或仅按整行):

return Orders
   .Select(ordeDetail => new {
       Detail = ordeDetail.OrderDetails
                    .GroupBy(w=>w.Product)
                    .Select(w=>new ProductSales {
                       Product=w.Key,
                       Total=w.Sum(prod=>prod.Quantity) })
   })
   .ToList();
于 2012-06-15T14:51:13.107 回答