欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

轻松掌握 LINQ Sum函数的使用方法

最编程 2024-08-10 17:25:21
...

LINQ Sum

LINQ总和简介

LINQ sum是聚合函数下的一个方法;该方法用于计算集合或列表中存在的数字值的总和或总数。这种查询语法支持VB.Net,而在C#中,它在Queryable和Enumerable类中可用。扩展方法Enumerable.Sum来自命名空间System.Linq。它的返回值是集合或列表中数字值的总和。它适用于int, decimal, double, float, nullable, non-nullable的数值。

语法。

下面是提到的语法。

int[] _sumValues = { 10,20,30,40,50 }; int _result = _ sumValues .Sum(); var _listValues= new List<int>{2,4,6,8}; int _result= _ listValues.sum();

Sum在LINQ中是如何工作的?

sum()方法是Enumerable.Sum的扩展,包含在System.Linq命名空间中。sum()函数计算存在于列表或集合中的项目的总和,并将数字值的总和作为结果返回。它适用于int, decimal, double, float, nullable, non-nullable的数值。

让我们通过几个例子来看看它的工作流程。

首先,我们从整数开始,从整数列表中获得数值之和。

var _numValues = new List<int> {2,3,4,5}; int _result = _ numValues.Sum(); // Output – 14

接下来,我们从十进制数开始,从十进制数的列表中得到数值的总和。

var _numValues = new List<decimal> {3.1m, 3.2m, 4.1m, 5.3m}; int _result = _ numValues.Sum(); // Output – 15.7

让我们看看方法中的空集合,在空集合上调用总和,其返回值为0。

var _numValues = new List<int> (); // empty list int _result = _ numValues.Sum(); // Output – 0

如果集合中存在可忽略的整数,预期的结果将是什么,让我们看看可忽略的整数列表中的数值之和。

var _numValues = new List<int> {4, 7, null, 3}; int _result = _ numValues.Sum(); // Output – 14

通过使用LINQ查询表达式,我们可以得到一个集合中数值的总和。

var listV = new List<int> {4, 4, 2, 4, 2, 4}; int _result = (from i in listV select i) .Sum(); // Output – 20

LINQ和的例子

下面给出的是提到的例子。

sum()方法是Enumerable.Sum的扩展,包含在System.Linq命名空间中。sum()函数计算列表或集合中的项目之和,并返回数值之和作为结果。

例子 #1

代码。

using System; using System. Linq; namespace Example_1 { class Program_A { static void Main(string[] args) { int[] _numValues = { 5, 10, 15, 20, 25 }; Console.WriteLine("LINQ Sum() Method:\n"); int _result = _numValues.Sum(); //_numValues.Sum() is used to add the values Console.WriteLine("To calculating the sum of elements in the list:"); Console.WriteLine("Sum is {0}", _result); Console.ReadLine(); } } }

解释。

  • 上面的程序解释了sum函数是用来计算列表 "numValues "中的元素之和的。

输出。

LINQ Sum 1

例子 #2

代码:

using System; using System. Linq; namespace Example_2 { class Program_B { static void Main(string[] args) { int[] _numValues = { 15, 10, 8, 9, 7 }; Console.WriteLine("LINQ Sum() - Using Query Method:\n"); //by using Query method int _query = (from num in _numValues select num).Sum(); Console.WriteLine("To calculate the sum of elements using Query Syntax:"); Console.WriteLine("Result is: " + _query); Console.ReadLine(); } } }

输出。

LINQ Sum 2

例子 #3

在下面的程序中,该方法同时应用于查询和方法格式,其中解释了雇员的详细信息;sum()被用来计算使用两种格式的雇员的工资。

代码:

using System; using System.Linq; using System.Collections.Generic; namespace LINQDemo { public class Employee { public int empID_ { get; set; } public string Employee_Name { get; set; } public int Employee_Salary { get; set; } public string Employee_Department { get; set; } public static List<Employee> EmployeeDetails() { List<Employee> listStudents = new List<Employee>() { new Employee{ empID_ = 1001,Employee_Name = "Smith", Employee_Salary = 25000, Employee_Department = "Networking"}, new Employee{ empID_ = 1002,Employee_Name = "Prem", Employee_Salary = 35000, Employee_Department = " Designing "}, new Employee{ empID_= 1003,Employee_Name = "Peter", Employee_Salary = 50000, Employee_Department = " Designing "}, new Employee{ empID_ = 1004,Employee_Name = "Rock", Employee_Salary = 20000, Employee_Department = " Networking "}, new Employee{ empID_ = 1005,Employee_Name = "Ajmal", Employee_Salary = 35000, Employee_Department = " Networking "}, new Employee{ empID_ = 1006,Employee_Name = "Sijae", Employee_Salary = 25000, Employee_Department = " Networking "}, new Employee{ empID_ = 1007,Employee_Name = "Shasha", Employee_Salary = 38000, Employee_Department = " Networking "}, new Employee{ empID_ = 1008,Employee_Name = "James", Employee_Salary = 18000, Employee_Department = "Designing"}, new Employee{ empID_= 1009,Employee_Name = "Prethiv", Employee_Salary = 52000, Employee_Department = " Designing "}, new Employee{ empID_ = 1010,Employee_Name = "Angath", Employee_Salary = 32000, Employee_Department = " Designing "} }; return listStudents; } static void Main(string[] args) { var totalSalary_method = Employee.EmployeeDetails() .Sum(emp => emp.Employee_Salary ); var totalSalary_query = (from emp in Employee.EmployeeDetails() select emp).Sum(e => e.Employee_Salary ); Console.WriteLine("\nLINQ Sum() in Query and Method Format\n"); Console.WriteLine("\nEMPLOYEE DETAILS"); Console.WriteLine("Sum Of Employee Salary = " + totalSalary_method); //Using Method Syntax var total_Salary = Employee.EmployeeDetails() .Where(emp => emp.Employee_Department == " Networking ") .Sum(emp => emp.Employee_Salary ); //Using Query Syntax var _totalSalary_QS = (from emp in Employee.EmployeeDetails() where emp.Employee_Department == " Networking " select emp).Sum(e => e.Employee_Salary ); Console.WriteLine("\nUsing Query Syntax"); Console.WriteLine("Total Salary of Networking Department " + _totalSalary_QS); var _totalSalary_MS = Employee.EmployeeDetails() .Where(emp => emp.Employee_Department == " Networking ") .Sum(emp => emp.Employee_Salary ); //method format var totalSalary_res = (from emp in Employee.EmployeeDetails() where emp.Employee_Department == " Networking " select emp).Sum(e => e.Employee_Salary ); //query format Console.WriteLine("\nUsing Method Syntax"); Console.WriteLine("Total Salary of Networking Department: " + _totalSalary_MS); Console.ReadKey(); } } }

输出。

with both query and method format

结论

在这篇文章中,我们看到了一个名为LINQ Sum()的聚合函数,它以查询和方法两种格式进行编程解释;通过使用一个方法,我们可以很容易地返回集合或列表中的数值之和。

推荐文章

这是一个关于LINQ Sum的指南。在这里,我们讨论了介绍,和的工作原理和例子,以便更好地理解。你也可以看看下面的文章来了解更多

  1. C#引用
  2. C#动作代理
  3. C#随机
  4. C# Await Async

The postLINQ Sumappeared first onEDUCBA.