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

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

      List&lt;int&gt; 中 int 的总和范围

      时间:2023-06-05

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

        <tbody id='UcI8j'></tbody>

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

                <legend id='UcI8j'><style id='UcI8j'><dir id='UcI8j'><q id='UcI8j'></q></dir></style></legend>
              • <tfoot id='UcI8j'></tfoot>
              • 本文介绍了List&lt;int&gt; 中 int 的总和范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我认为这将是微不足道的,但我不知道该怎么做.我有一个 List<int>,我想对一系列数字求和.

                I reckon this will be quite trivial but I can't work out how to do it. I have a List<int> and I want to sum a range of the numbers.

                假设我的清单是:

                var list = new List<int>()
                {
                    1, 2, 3, 4
                };
                

                如何获得前 3 个对象的总和?结果是 6.我尝试使用 Enumerable.Range 但无法让它工作,不确定这是否是最好的方法.

                How would I get the sum of the first 3 objects? The result being 6. I tried using Enumerable.Range but couldn't get it to work, not sure if that's the best way of going about it.

                不做:

                int sum = list[0] + list[1] + list[2];
                

                推荐答案

                您可以使用 采取 &总和:

                You can accomplish this by using Take & Sum:

                var list = new List<int>()
                {
                    1, 2, 3, 4
                };
                
                // 1 + 2 + 3
                int sum = list.Take(3).Sum(); // Result: 6
                

                如果您想对从其他地方开始的范围求和,可以使用 跳过:

                If you want to sum a range beginning elsewhere, you can use Skip:

                var list = new List<int>()
                {
                    1, 2, 3, 4
                };
                
                // 3 + 4
                int sum = list.Skip(2).Take(2).Sum(); // Result: 7
                

                或者,使用 OrderBy 重新排序您的列表a> 或 OrderByDescending 然后求和:

                Or, reorder your list using OrderBy or OrderByDescending and then sum:

                var list = new List<int>()
                {
                    1, 2, 3, 4
                };
                
                // 3 + 4
                int sum = list.OrderByDescending(x => x).Take(2).Sum(); // Result: 7
                

                如您所见,有多种方法可以完成此任务(或相关任务).请参阅 TakeSum, 跳过, OrderBy &OrderByDescending 文档了解更多信息.

                As you can see, there are a number of ways to accomplish this task (or related tasks). See Take, Sum, Skip, OrderBy & OrderByDescending documentation for further information.

                这篇关于List&lt;int&gt; 中 int 的总和范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 C# 中对两个字典中的值求和? 下一篇:使用 SUM 和 ORDER BY 进行 Linq 查询

                相关文章

                最新文章

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

              • <legend id='NGoUe'><style id='NGoUe'><dir id='NGoUe'><q id='NGoUe'></q></dir></style></legend>

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

              • <tfoot id='NGoUe'></tfoot>