获取Dictionary>()

本文关键字:int List string Dictionary 获取 | 更新日期: 2023-09-27 18:12:56

如何得到每个列表中所有整数的和?

代码:

var teamScoreDict = new Dictionary<string, List<int>>()

示例数据:

teamScoreDict.Add("Blue", new List<int>(){34,53,57,22,68,78,12,28});
 teamScoreDict.Add("Red", new List<int>(){64,66,22,73,25,45,36,21});

获取Dictionary<string中int值的总和,List<int>>()

这将创建一个新的字典,包含团队名称和列表中所有值的总和

teamScoreDict.ToDictionary(k => k.Key, v => v.Value.Sum())
var sum = teamScoreDict.SelectMany(x => x.Value).Sum();