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;

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);

}
}

```

## 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;

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 };
return lstEmployee;
}

static void Main(string[] args)
{
double dAverageSalary = DataSource().Average(e => e.Salary);
Console.WriteLine("Sum of Salary {0}",Math.Round(dAverageSalary,2));

}
}

```

## 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;

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 };
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));

}
}```

## Conclusion:

I hope you would love this post. Please feel free to comment for any questions. Your feedback and suggestions are welcome.

Thank You.

## 1 Comment

• 85737 132750Hey there! Great post! Please when all could see a follow up! 731733