how to get a SUM in Linq?

后端 未结 3 2190
旧巷少年郎
旧巷少年郎 2020-12-06 01:17

I need to do the following, I have a List with a class which contains 2 integer id and count

Now I want to do the following linq query:

         


        
3条回答
  •  猫巷女王i
    2020-12-06 02:03

    Group the items by Id and then sum the Counts in each group:

    var result = items.GroupBy(x => x.Id)
                      .Select(g => new { Id = g.Key, Sum = g.Sum(x => x.Count) });
    

提交回复
热议问题