在LINQ中,sum()函数用于计算集合/列表中项目的总和。
下面是在C#中定义LINQ SUM函数的语法,总而言之,列表中的项。
int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int Sum = Num.Sum();
From the above syntax, we are summing up the items in the "Num" list using the LINQ Sum function.
现在,我们将看到使用LINQ Sum()函数汇总C#应用程序中集合/列表中的项的完整示例。
using System; using System.Collections.Generic; using System. Linq; using System. Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //create array num with the initializing value int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Console.WriteLine("Calculating the sum of all the elements of the array :"); //Num.Sum() is used to add the value of the Num array int Sum = Num.Sum(); Console.WriteLine("The Sum is {0}", Sum); Console.ReadLine(); } } }
In the above example, we used the sum function to calculate the sum of the elements in the array list "Num."
链接:https://www.learnfk.comhttps://www.learnfk.com/linq/linq-sum-function.html
来源:LearnFk无涯教程网
当我们执行上面的示例时,我们会得到如下所示的结果:
我们可以使用LINQ中的sum()函数来汇总集合/列表中的项目,这基于我们的要求。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)