Summarize list by property

前端 未结 3 1523
小鲜肉
小鲜肉 2021-01-27 16:48

I have a list of objects like this:

List list = new List();
list.Add(new ClassName()
{
    Name = 1,
    Amount = 5
});
list.Ad         


        
3条回答
  •  不思量自难忘°
    2021-01-27 17:00

     var lst = list.GroupBy(g = > g.Name).Select(t = > new
     ClassName()
     {
         Amount = t.Sum(s=>s.Amount), Name = t.Key
     }).ToList();
    

提交回复
热议问题