C#循环遍历字典并对键的值求和

C#循环遍历字典并对键的值求和,c#,dictionary,C#,Dictionary,我有一本字典,我希望能够用不同的字符串对小数进行求和。假设我在字典中有以下输入 “1”,20.00 "1", 35.00 "2", 10.00 "3", 15.00 “3”,30.00 我希望将以下输出添加到新词典中 “1”,55.00 "2", 10.00 “3”,45.00 我猜是这样的 foreach(var item in dictionary) { newDictionary.Add(not sure what would go here, maybe some sort of lin

我有一本
字典
,我希望能够用不同的字符串对小数进行求和。假设我在字典中有以下输入

“1”,20.00 "1", 35.00 "2", 10.00 "3", 15.00 “3”,30.00

我希望将以下输出添加到新词典中

“1”,55.00 "2", 10.00 “3”,45.00

我猜是这样的

foreach(var item in dictionary)
{
newDictionary.Add(not sure what would go here, maybe some sort of linq query for distinct and sum?);
}

字典中的键不能重复,因此前两个条目在字典中不合适

我认为您可能有一个可以循环的对象列表,然后您可以使用字典存储每个“键”的总数

差不多

 Dictionary<string, double> totals = new Dictionary<string, double>();
        List<Tuple<string, double>> entries = new List<Tuple<string, double>>() {
            new Tuple<string, double>("1",20.00),
            new Tuple<string, double>("1",35.00),
            new Tuple<string, double>("2",10.00),
            new Tuple<string, double>("3",15.00),
            new Tuple<string, double>("3",30.00)
        };
        foreach (var e in entries)
        {
            if (!totals.Keys.Contains(e.Item1))
            {
                totals[e.Item1] = 0;
            }
            totals[e.Item1] += e.Item2;
        }
字典总计=新字典();
列表条目=新列表(){
新元组(“1”,20.00),
新元组(“1”,35.00),
新元组(“2”,10.00),
新元组(“3”,15.00),
新元组(“3”,30.00)
};
foreach(条目中的变量e)
{
如果(!totals.Keys.Contains(e.Item1))
{
总计[e.Item1]=0;
}
总计[e.Item1]+=e.Item2;
}

不能让Dictionary对象具有重复的键。当您尝试将现有键添加到Dictionary对象中时,将看到ArgumentException。
请参阅:

如本文所述,如果使用相同的键,则不能使用字典,因为它们必须是唯一的。 您可以使用最接近字典的密钥对列表,然后您的代码将如下所示:

        List<KeyValuePair<string, decimal?>> myList = new List<KeyValuePair<string, decimal?>>();

        myList.Add(new KeyValuePair<string, decimal?>("1", (decimal)10.00));
        myList.Add(new KeyValuePair<string, decimal?>("1", (decimal)15.00));
        myList.Add(new KeyValuePair<string, decimal?>("3", (decimal)30.50));
        myList.Add(new KeyValuePair<string, decimal?>("3", (decimal)17.500));

        Dictionary<string, decimal?> sums = new Dictionary<string, decimal?>(); //This can be a dictionary because the keys will be unique after grouping 

        foreach (var item in myList.GroupBy(m => m.Key))
        {
            string currentKey = item.FirstOrDefault().Key;
            sums.Add(currentKey, myList.Where(j => j.Key == currentKey).Sum(o => o.Value));
        }
List myList=new List();
添加(新的KeyValuePair(“1”,(十进制)10.00));
添加(新的KeyValuePair(“1”,(十进制)15.00));
添加(新的KeyValuePair(“3”,(十进制)30.50));
添加(新的KeyValuePair(“3”,十进制)17.500);
字典总和=新字典()//这可以是一个字典,因为分组后键是唯一的
foreach(myList.GroupBy中的var项(m=>m.Key))
{
字符串currentKey=item.FirstOrDefault().Key;
Add(currentKey,myList.Where(j=>j.Key==currentKey).Sum(o=>o.Value));
}

假设与其他答案中的键值对列表相同:

var myList = New List<KeyValuePair<string, decimal?>> {
    new KeyValuePair<string, decimal?>("1", (decimal)10.00),
    new KeyValuePair<string, decimal?>("1", (decimal)15.00),
    new KeyValuePair<string, decimal?>("2", (decimal)20.00),
    new KeyValuePair<string, decimal?>("3", (decimal)30.50),
    new KeyValuePair<string, decimal?>("3", (decimal)17.500)
};

var myResults = myList.GroupBy(p => p.Key)
                      .ToDictionary(g => g.Key, g=>g.Sum(p=>p.Value))
var myList=新列表{
新的KeyValuePair(“1”,(十进制)10.00),
新的KeyValuePair(“1”,(十进制)15.00),
新的KeyValuePair(“2”,(十进制)20.00),
新的KeyValuePair(“3”,(十进制)30.50),
新的KeyValuePair(“3”(十进制)17.500)
};
var myResults=myList.GroupBy(p=>p.Key)
.ToDictionary(g=>g.Key,g=>g.Sum(p=>p.Value))

字典必须有唯一的键。e、 g.
{{“1”,20.00},{“1”,35.00}
将不可能在向第一个字典添加输入时执行该操作。
dict[“1”]=20.00;dict[“1”]+=35.00
工作正常。在任何时候都不会有两个值具有相同的键,您只是在更新现有键的值。字典不会像输出那样作为输入。如果使用元组,本文中的一些信息可能会有用:GroupBy已经为您完成了所有分组(因此它的名称)。组集合中的每个组都有一个键(无需执行firstOrDefault),并且已经是匹配该键的KeyValuePairs集合。因此,您可以直接求和,而无需重复查询原始列表。看看我的答案。