我还没有找到任何关于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文本):
有人可以帮助吗?
我不认为你需要'select'部分。但我可能会误解这个要求。你能解释究竟应该是什么结果? – Nisarg
什么是错误/ s? –