 GeeksforGeeks App
Open App Browser
Continue

# C# Program to Print Employees Whose Salary is Greater than Average of all Employees Salaries Using LINQ

LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this article, we will learn how to display a list of those employees’ details whose salary is greater than the average of all employee salaries.

Example:

```Input:
{{emp_Name = "sravan", emp_Age = 21, emp_Salary = 26199},
{emp_Name = "ramya", emp_Age = 22, emp_Salary = 19000},
{emp_Name = "harsha", emp_Age = 23, emp_Salary = 25000},
{emp_Name = "deepu", emp_Age = 23, emp_Salary = 23456},
{emp_Name = "jyothika", emp_Age = 21, emp_Salary = 21345}}
Output:
{emp_Name = "sravan", emp_Age = 21, emp_Salary = 26199}
{emp_Name = "harsha", emp_Age = 23, emp_Salary = 25000}
{emp_Name = "deepu", emp_Age = 23, emp_Salary = 23456}

Input:
{{emp_Name = "sravan", emp_Age = 21, emp_Salary = 26199},
{emp_Name = "ramya", emp_Age = 22, emp_Salary = 19000}}
Output:
{emp_Name = "sravan", emp_Age = 21, emp_Salary = 26199}```

Approach:

1. Declare three variables- employee name, age, and salary
2. Create a list of employees using list collection.
3. Using Query compute the average salary of all the employees.
4. Using where clause check each employee salary which is greater than average value.
5. Select that employees.
6. Display the result using ToString() method.

Example:

## C#

 `// C# program to display the list of employees``// whose salary is greater than average salary``// of all the employees``using` `System;``using` `System.Collections.Generic;``using` `System.Linq;` `class` `Employee{` `// Declare three variables``// employee name, age, and Salary``string` `emp_Name;``int` `emp_Age;``int` `emp_Salary;` `// Get the data``public` `override` `string` `ToString()``{``    ``return` `" "` `+ emp_Name + ``" "` `+``                  ``emp_Age + ``" "` `+ emp_Salary;``}` `// Driver code``static` `void` `Main(``string``[] args)``{``    ` `    ``// Create the list of 5 employees``    ``List emp = ``new` `List()``    ``{``        ``new` `Employee{ emp_Name = ``"sravan"``, emp_Age = 21,``                      ``emp_Salary = 26199 },``        ``new` `Employee{ emp_Name = ``"ramya"``, emp_Age = 22,``                      ``emp_Salary = 19000 },``        ``new` `Employee{ emp_Name = ``"harsha"``, emp_Age = 23,``                      ``emp_Salary = 25000 },``        ``new` `Employee{ emp_Name = ``"deepu"``, emp_Age = 23,``                      ``emp_Salary = 23456 },``        ``new` `Employee{ emp_Name = ``"jyothika"``, emp_Age = 21,``                      ``emp_Salary = 21345 }``    ``};` `    ``IEnumerable result =``    ` `    ``// Getting the average of all the employees``    ``from` `e ``in` `emp``    ` `    ``// Total``    ``let` `total = emp.Sum(sal => sal.emp_Salary)``    ``let` `average = total / 5``    ` `    ``// Condition to get salary greater than``    ``// average of all employees``    ``where` `e.emp_Salary > average``    ` `    ``// Select that employees``    ``select` `e;` `    ``Console.WriteLine(``" Employee-Name Employee-Age Employee-Salary"``);``    ``foreach` `(Employee x ``in` `result)``    ``{``        ``Console.WriteLine(x.ToString());``    ``}``}``}`

Output:

```Employee-Name Employee-Age Employee-Salary
sravan 21 26199
harsha 23 25000
deepu 23 23456```

My Personal Notes arrow_drop_up