Hi friends, In this tutorial, we will see the Average method in Linq. An average is an extension method in System.Linq namespace to compute the average of the numeric values in a collection.
An average method in Linq:
Average function is used to compute the average of numeric values in a collection.
Example1: Calculate the average of numbers in a collection.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { int[] iNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; double Average = iNumbers.Average(); Console.WriteLine("Average is {0}", Average); Console.ReadLine(); } }
Output:
Average is 5
Example 2: Calculate the average salary using the Method syntax:
This example will calculate the average salary of the employee.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class Employee { public string EmployeeCode { get; set; } public string EmployeeName { get; set; } public string Gender { get; set; } public string Designation { get; set; } public double Salary { get; set; } } class Program { public static List<Employee> DataSource() { List<Employee> lstEmployee = new List<Employee>(); Employee objEmployee1 = new Employee() { EmployeeCode = "E001", EmployeeName = "Ayush Singh", Gender = "Male", Designation = "Software Engineer", Salary = 30000 }; Employee objEmployee2 = new Employee() { EmployeeCode = "E002", EmployeeName = "Ritwik Nigam", Gender = "Male", Designation = "Software Engineer", Salary = 30000 }; Employee objEmployee3 = new Employee() { EmployeeCode = "E003", EmployeeName = "Nikhil Bakshi", Gender = "Male", Designation = "Team Lead", Salary = 60000 }; Employee objEmployee4 = new Employee() { EmployeeCode = "E004", EmployeeName = "Nidhi Verma", Gender = "Female", Designation = "HR", Salary = 40000 }; Employee objEmployee5 = new Employee() { EmployeeCode = "E005", EmployeeName = "Ayushman Lakhotia", Gender = "Male", Designation = "Project Manager", Salary = 80000 }; Employee objEmployee6 = new Employee() { EmployeeCode = "E006", EmployeeName = "Neha Sharma", Gender = "Female", Designation = "Graphic Designer", Salary = 25000 }; lstEmployee.Add(objEmployee1); lstEmployee.Add(objEmployee2); lstEmployee.Add(objEmployee3); lstEmployee.Add(objEmployee4); lstEmployee.Add(objEmployee5); lstEmployee.Add(objEmployee6); return lstEmployee; } static void Main(string[] args) { double dAverageSalary = DataSource().Average(e => e.Salary); Console.WriteLine("Sum of Salary {0}",Math.Round(dAverageSalary,2)); Console.ReadLine(); } }
Output:
Sum of Salary 44166.67
Example 3: Calculate the average salary using Query Syntax.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class Employee { public string EmployeeCode { get; set; } public string EmployeeName { get; set; } public string Gender { get; set; } public string Designation { get; set; } public double Salary { get; set; } } class Program { public static List<Employee> DataSource() { List<Employee> lstEmployee = new List<Employee>(); Employee objEmployee1 = new Employee() { EmployeeCode = "E001", EmployeeName = "Ayush Singh", Gender = "Male", Designation = "Software Engineer", Salary = 30000 }; Employee objEmployee2 = new Employee() { EmployeeCode = "E002", EmployeeName = "Ritwik Nigam", Gender = "Male", Designation = "Software Engineer", Salary = 30000 }; Employee objEmployee3 = new Employee() { EmployeeCode = "E003", EmployeeName = "Nikhil Bakshi", Gender = "Male", Designation = "Team Lead", Salary = 60000 }; Employee objEmployee4 = new Employee() { EmployeeCode = "E004", EmployeeName = "Nidhi Verma", Gender = "Female", Designation = "HR", Salary = 40000 }; Employee objEmployee5 = new Employee() { EmployeeCode = "E005", EmployeeName = "Ayushman Lakhotia", Gender = "Male", Designation = "Project Manager", Salary = 80000 }; Employee objEmployee6 = new Employee() { EmployeeCode = "E006", EmployeeName = "Neha Sharma", Gender = "Female", Designation = "Graphic Designer", Salary = 25000 }; lstEmployee.Add(objEmployee1); lstEmployee.Add(objEmployee2); lstEmployee.Add(objEmployee3); lstEmployee.Add(objEmployee4); lstEmployee.Add(objEmployee5); lstEmployee.Add(objEmployee6); return lstEmployee; } static void Main(string[] args) { double dAverageSalary = (from e in DataSource() select e.Salary).Average(); Console.WriteLine("Sum of Salary {0}",Math.Round(dAverageSalary,2)); Console.ReadLine(); } }
View More:
- Count operator in Linq.
- Any Operator in Linq.
- SelectMany: LINQ projection operator.
- Where Clause Filtering Operator in Linq
Conclusion:
I hope you would love this post. Please feel free to comment for any questions. Your feedback and suggestions are welcome.
Thank You.