C#字典中值的求和性

时间:2015-01-14 11:00:49

标签: c# dictionary

我有类似的东西

Dictionary<String, Profile > sto;

其中Profile是具有不同属性的类(例如int Ints)。现在我想知道是否有一个命令可以对整个字典中的所有Int求和。

2 个答案:

答案 0 :(得分:3)

您可以使用Sum方法:

int sum = sto.Sum(x => x.Value.SomeIntProperty);

答案 1 :(得分:2)

您可以尝试这样的事情:

var sum = sto.Sum(x=>x.Value.PropertyName);

PropertyNameProfile类的属性,类型为int

相关问题