我如何获得每个列表中所有整数的总和?
代码:
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});
答案 0 :(得分:3)
var sum = teamScoreDict.SelectMany(x => x.Value).Sum();
答案 1 :(得分:3)
这应该是一个新词典,包括团队名称和列表中所有值的总和
teamScoreDict.ToDictionary(k => k.Key, v => v.Value.Sum())