<i id='ETn8M'><tr id='ETn8M'><dt id='ETn8M'><q id='ETn8M'><span id='ETn8M'><b id='ETn8M'><form id='ETn8M'><ins id='ETn8M'></ins><ul id='ETn8M'></ul><sub id='ETn8M'></sub></form><legend id='ETn8M'></legend><bdo id='ETn8M'><pre id='ETn8M'><center id='ETn8M'></center></pre></bdo></b><th id='ETn8M'></th></span></q></dt></tr></i><div id='ETn8M'><tfoot id='ETn8M'></tfoot><dl id='ETn8M'><fieldset id='ETn8M'></fieldset></dl></div>

      <small id='ETn8M'></small><noframes id='ETn8M'>

        <bdo id='ETn8M'></bdo><ul id='ETn8M'></ul>
      <tfoot id='ETn8M'></tfoot>
    1. <legend id='ETn8M'><style id='ETn8M'><dir id='ETn8M'><q id='ETn8M'></q></dir></style></legend>
    2. 如何在 C# 中对两个字典中的值求和?

      时间:2023-06-05

            <legend id='dyIk9'><style id='dyIk9'><dir id='dyIk9'><q id='dyIk9'></q></dir></style></legend>

                <bdo id='dyIk9'></bdo><ul id='dyIk9'></ul>
              • <tfoot id='dyIk9'></tfoot>

                <i id='dyIk9'><tr id='dyIk9'><dt id='dyIk9'><q id='dyIk9'><span id='dyIk9'><b id='dyIk9'><form id='dyIk9'><ins id='dyIk9'></ins><ul id='dyIk9'></ul><sub id='dyIk9'></sub></form><legend id='dyIk9'></legend><bdo id='dyIk9'><pre id='dyIk9'><center id='dyIk9'></center></pre></bdo></b><th id='dyIk9'></th></span></q></dt></tr></i><div id='dyIk9'><tfoot id='dyIk9'></tfoot><dl id='dyIk9'><fieldset id='dyIk9'></fieldset></dl></div>
                  <tbody id='dyIk9'></tbody>
              • <small id='dyIk9'></small><noframes id='dyIk9'>

                本文介绍了如何在 C# 中对两个字典中的值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我有两个结构相同的字典:

                I have two dictionaries with the same structure:

                Dictionary<string, int> foo = new Dictionary<string, int>() 
                {
                    {"Table", 5 },
                    {"Chair", 3 },
                    {"Couch", 1 }
                };
                
                Dictionary<string, int> bar = new Dictionary<string, int>() 
                {
                    {"Table", 4 },
                    {"Chair", 7 },
                    {"Couch", 8 }
                };
                

                我想将字典的值相加并返回带有键的第三个字典,以及每个键的总值:

                I'd like to sum the values of the dictionaries together and return a third dictionaries with the keys, and the total values for each key:

                Table, 9
                Chair, 10
                Couch, 9
                

                我目前的解决方案是遍历字典并以这种方式将它们拉出,但我知道该解决方案不是性能最高或可读性最强的解决方案.但是,我在尝试在 LINQ 中提出解决方案时遇到了困难.

                My current solution is to loop through the dictionary and pull them out that way, but I know that solution isn't the most performant or most readable. However, I'm hitting a brick wall trying to come up with a solution in LINQ.

                推荐答案

                以下不是最有效的解决方案(因为它只是将两个字典都视为可枚举),但它会起作用并且很清楚:

                The following isn't the most efficient solution (because it simply treats both dictionaries as enumerables), but it will work and it is quite clear:

                Dictionary<string, int> result = (from e in foo.Concat(bar)
                              group e by e.Key into g
                              select new { Name = g.Key, Count = g.Sum(kvp => kvp.Value) })
                              .ToDictionary(item => item.Name, item => item.Count);
                

                这篇关于如何在 C# 中对两个字典中的值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Linq 按日期获取数据组的总和 下一篇:List&lt;int&gt; 中 int 的总和范围

                相关文章

                最新文章

                  <bdo id='QGOvJ'></bdo><ul id='QGOvJ'></ul>

                  <small id='QGOvJ'></small><noframes id='QGOvJ'>

                  <i id='QGOvJ'><tr id='QGOvJ'><dt id='QGOvJ'><q id='QGOvJ'><span id='QGOvJ'><b id='QGOvJ'><form id='QGOvJ'><ins id='QGOvJ'></ins><ul id='QGOvJ'></ul><sub id='QGOvJ'></sub></form><legend id='QGOvJ'></legend><bdo id='QGOvJ'><pre id='QGOvJ'><center id='QGOvJ'></center></pre></bdo></b><th id='QGOvJ'></th></span></q></dt></tr></i><div id='QGOvJ'><tfoot id='QGOvJ'></tfoot><dl id='QGOvJ'><fieldset id='QGOvJ'></fieldset></dl></div>
                1. <tfoot id='QGOvJ'></tfoot>

                2. <legend id='QGOvJ'><style id='QGOvJ'><dir id='QGOvJ'><q id='QGOvJ'></q></dir></style></legend>