2017-08-02 61 views
0

我還沒有找到任何關於LINQ Group的這個具體問題的答案&聚合,所以我希望有人在這裏可以提供幫助。我有這樣的機型列表:LINQ Group&Sum其他模型的模型列表

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

然而,它嚴重地不喜歡這個(按紅色squigly行和偶數red'er文本):

enter image description here

有人可以幫助嗎?

+1

我不認爲你需要'select'部分。但我可能會誤解這個要求。你能解釋究竟應該是什麼結果? – Nisarg

+1

什麼是錯誤/ s? –

回答

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(); 
0

嘗試以下操作:

  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 
0

當您指定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(); 
1
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(); 
0

試試這個 名單allBasketProducts =新名單();

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