我有一个包含2个字段的库存模型列表。例如。
库存模式
public class stock
{
public int Id{get;set;}
public string product{get;set;}
public decimal stock{get;set;}
}
我想通过linq.any帮助首先按产品分类和总计代码?
答案 0 :(得分:2)
假设您拥有这些Stock
个对象的集合,您可以使用Select()
方法以及GroupBy()
和Sum()
来检索您要查找的内容:
// This will group each of your elements by their product value and project each of these
// groups to an object that stores the product and the sum of the stock properties for that
// group.
var totals = products.GroupBy(p => p.product)
.Select(p => new { Product = p.Key, Stock = p.Sum(x => x.stock) })
.ToList();