C# 如何从SortedDictionary获取整数值的运行总计? SortedDictionary类型字典=新的SortedDictionary(); SortedDictionary Length字典=新的SortedDictionary();

C# 如何从SortedDictionary获取整数值的运行总计? SortedDictionary类型字典=新的SortedDictionary(); SortedDictionary Length字典=新的SortedDictionary();,c#,sorting,dictionary,sum,C#,Sorting,Dictionary,Sum,lengthDictionary在键值对中具有如下值: SortedDictionary<int, string> typeDictionary = new SortedDictionary<int, string>(); SortedDictionary<int, int> lengthDictionary = new SortedDictionary<int, int>(); 我想要LINQ查询,它将返回如下所示的新列表 <1,2

lengthDictionary在键值对中具有如下值:

SortedDictionary<int, string> typeDictionary = new SortedDictionary<int, string>(); 
SortedDictionary<int, int> lengthDictionary = new SortedDictionary<int, int>();

我想要LINQ查询,它将返回如下所示的新列表

<1,20>
<2,8>
<3,10>
<4,5>

//键1中的20
//28来自键2
//键3中的38
结果应该是这样的:

<1,20>
<2,20+8=28>  // 20 from key 1
<3,28+10=38> // 28 from key 2
<4,38+5=43>  // 38 from key 3

如果您需要一个查询,而不一定需要一个新的排序字典,您可以构造如下内容

<1,20>
<2,28>
<3,38>
<4,43>
int runningSum=0;
变量查询=(来自lengthDictionary中的对)
让innerSum=(runningSum+=pair.Value)
选择新的
{
Key=pair.Key,
值=内和
});
//做一个新的分类词典?
var newDictionary=newsorteddictionary(query.ToDictionary(pair=>pair.Key,pair=>pair.Value));
//显示以确认结果
foreach(newDictionary中的var对)
WriteLine(“{0}\t{1}”,pair.Key,pair.Value);
注意:非LINQ答案可能更简单,特别是如果您想要一本新词典作为输出

int runningSum = 0;
var query = (from pair in lengthDictionary
            let innerSum = (runningSum += pair.Value)
            select new
            {
                Key = pair.Key,
                Value = innerSum 
            });

// make a new SortedDictionary?
var newDictionary = new SortedDictionary<int, int>(query.ToDictionary(pair => pair.Key, pair => pair.Value));

// display to confirm result
foreach (var pair in newDictionary)
    Console.WriteLine("{0}\t{1}", pair.Key, pair.Value);
var newDictionary=newsorteddictionary();
int runningTotal=0;
foreach(lengthDictionary中的变量对)
{
runningTotal+=pair.Value;
newDictionary.Add(pair.Key,runningTotal);
}
以下是我的方法:

int runningTotal = 0;
lengthDictionary.Select(p => 
                          {
                              runningTotal += p.Value; 
                              return new {Key = p.Key, Value = runningTotal};
                           });
SortedDictionary字典=新的SortedDictionary()
{
{1, 20},
{2, 8},
{3, 10},
{4, 5}
};
var结果=可枚举
.Range(0,dictionary.Keys.Count)
.选择(i=>new
{
Key=dictionary.Keys.Skip(i).First(),
Value=dictionary.Take(i+1).选择(k=>k.Value).Sum()
})
.ToDictionary(k=>k.Key,v=>v.Value);
foreach(结果中的var)
{
WriteLine(“Key={0},Value={1}”,r.Key,r.Value);
}
控制台。写入线(“完成”);
Console.ReadLine();

它必须是LINQ吗?或者你只是对得到结果感到高兴?我仍然很困惑
typeDictionary
的作用在哪里。一个简短但完整的例子确实会有帮助——比如,不要考虑Type字典…LINQ会被其他逻辑解决。“用户”不要考虑Type字典。“……那么就不应该在你的问题中了。+ 1和I修改了你的答案,使返回类型<代码>字典< /代码>。希望您不介意:)从字典返回项目的顺序未定义,可能不是用户578083所要的。@Jonas Elfstrom,lengthDictionary不是字典。
int tempTotal = 0;
Dictionary<int, int> result = lengthDictionary.ToDictionary(p => p.Key, p =>
                                                 {
                                                      tempTotal += p.Value;
                                                      return tempTotal;
                                                 });
int runningTotal = 0;
lengthDictionary.Select(p => 
                          {
                              runningTotal += p.Value; 
                              return new {Key = p.Key, Value = runningTotal};
                           });
SortedDictionary<int, int> dictionary = new SortedDictionary<int, int>()
                                            {
                                                {1, 20},
                                                {2, 8},
                                                {3, 10},
                                                {4, 5}
                                            };


var result = Enumerable
    .Range(0, dictionary.Keys.Count)
    .Select(i => new
                        {
                            Key = dictionary.Keys.Skip(i).First(),
                            Value = dictionary.Take(i+1).Select(k => k.Value).Sum()
                        })
    .ToDictionary(k => k.Key, v => v.Value);



foreach(var r in result)
{
    Console.WriteLine("Key = {0}, Value = {1}", r.Key, r.Value);
}


Console.WriteLine("done");

Console.ReadLine();