【问题标题】:LINQ Group & Sum on a list of Models where a property is another model属性是另一个模型的模型列表上的 LINQ 组和总和
【发布时间】:2017-08-02 17:01:11
【问题描述】:

我无法在 LINQ Group & Aggregation 上找到此特定问题的任何答案,因此希望这里有人能提供帮助。我有一个这样的模型列表:

public class BasketProduct
{
    public ProductItem  Product { get; set; }
    public int          Quantity { get; set; }
    public decimal      SubTotal { get; set; }
    public DateTime     DateAdded { get; set; }
}

第一个属性是另一个模型:

public class ProductItem
{
    public int      ID { get; set; }
    public string   Description { get; set; }
    public char     Item { get; set; }
    public decimal  Price { get; set; }
    public string   ImagePath { get; set; }
    public string   Barcode { get; set; }
}

我基本上希望能够在此列表上进行分组和聚合:

List<BasketProduct> allBasketProducts = 

使用以下内容:

        allBasketProducts = allBasketProducts
            .GroupBy(x => x.Product.ID)
            .Select(y => new BasketProduct
                                        {
                                            Product.ID = y.First().Product.ID,
                                            Product.Item = y.First().Product.Item,
                                            Product.Description = y.First().Product.Description,
                                            Quantity = y.Sum(z => z.Quantity),
                                            Product.ImagePath = y.First().Product.ImagePath,
                                            Product.Price = y.First().Product.Price,
                                            SubTotal = y.Sum(z => z.SubTotal)
                                        }).ToList();

但是它真的不喜欢这样(根据红色波浪线甚至更红的文字):

有人可以帮忙吗?

【问题讨论】:

  • 我认为您不需要 select 部分。但我可能误解了这个要求。你能解释一下究竟应该是什么结果吗?
  • 错误是什么?

标签: c# linq model group-by aggregate


【解决方案1】:

您的问题实际上与 LINQ 无关,而是您的 ProductItem 构造函数。您需要显式构造其嵌套的Product 对象,如下所示:

allBasketProducts
    .GroupBy(x => x.Product.ID)
    .Select(y => new BasketProduct
        {
            Quantity = y.Sum(z => z.Quantity),
            SubTotal = y.Sum(z => z.SubTotal),
            Product = new ProductItem
            {
                ID = y.First().Product.ID,
                Item = y.First().Product.Item,
                Description = y.First().Product.Description,
                ImagePath = y.First().Product.ImagePath,
                Price = y.First().Product.Price
            }
        }).ToList();

【讨论】:

    【解决方案2】:
    var totals = 
        (from b in allBasketProducts
        group new { b.Quantity, b.SubTotal, Product= b.Product } by b.Product.ID into g
        select new BasketProduct
        { 
            Product = g.First().Product,
            SubTotal = g.Sum(z => z.SubTotal), 
            Quantity = g.Sum(z => z.Quantity)
        }).ToList();
    

    【讨论】:

      【解决方案3】:

      尝试以下:

                  allBasketProducts = allBasketProducts  
                   .GroupBy(x => x.Product.ID)
                   .Select(y => new BasketProduct()
                   {
                       Product = new ProductItem() {
                           ID = y.First().Product.ID,
                           Item = y.First().Product.Item,
                           Description = y.First().Product.Description,
                           ImagePath = y.First().Product.ImagePath,
                           Price = y.First().Product.Price
                       },
                       Quantity = y.Sum(z => z.Quantity),
                       SubTotal
      

      【讨论】:

        【解决方案4】:

        当您指定 Select 的类型时,编译器只需要该类型的属性。所以你只能在你的代码中设置属性ProductSubtotalQuantityDateAdded

        您只需选择 ID 与您的分组键匹配的第一个产品即可找到该产品:

        var allBasketProductsGroupedByProductID = allBasketProducts
                                .GroupBy(x => x.Product.ID)
                                .Select(y => new BasketProduct
                                {
                                    Product = y.First(i => i.Product.ID == y.Key).Product,
                                    Quantity = y.Sum(z => z.Quantity),
                                    SubTotal = y.Sum(z => z.SubTotal)                   
                                }).ToList();
        

        【讨论】:

          【解决方案5】:

          试试这个 列出所有BasketProducts = new List();

                  allBasketProducts = new List<BasketProduct>()
                  {
                      new BasketProduct()
                      {
                          Product = new ProductItem()
                          {
                              ID = 1,
                              Price = 5,
                          },
                          Quantity = 2,
                          SubTotal = 2,
          
                      },
          
                      new BasketProduct()
                      {
                          Product = new ProductItem()
                          {
                              ID = 1,
                              Price = 5,
                          },
                          Quantity = 4,
                          SubTotal = 2,
          
                      },
                      new BasketProduct()
                      {
                          Product = new ProductItem()
                          {
                              ID = 2,
                              Price = 10,
                          },
                          Quantity = 3,
                          SubTotal = 2,
          
                      },
                      new BasketProduct()
                      {
                          Product = new ProductItem()
                          {
                              ID = 3,
                              Price = 20,
                          },
                          Quantity = 3,
                          SubTotal = 2,
          
                      },
                      new BasketProduct()
                      {
                          Product = new ProductItem()
                          {
                              ID = 2,
                              Price = 20,
                          },
                          Quantity = 3,
                          SubTotal = 2,
          
                      }
                  };
          
                  allBasketProducts = allBasketProducts
                      .GroupBy(x => x.Product.ID)
                      .Select(y => new BasketProduct()
                      {
                          Product = new ProductItem()
                          {
                              ID = y.First().Product.ID,
                              Item = y.First().Product.Item,
                              Description = y.First().Product.Description,
                              ImagePath = y.First().Product.ImagePath,
                              Price = y.First().Product.Price
                          },
                          Quantity = y.Sum(z => z.Quantity),
                          SubTotal = y.Sum(z => z.SubTotal)
                      }).ToList();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-27
            • 1970-01-01
            • 2016-03-30
            • 2016-07-21
            • 1970-01-01
            相关资源
            最近更新 更多