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

16 Oct, 2021
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:
Declare three variables- employee name, age, and salaryCreate a list of employees using list collection.Using Query compute the average salary of all the employees.Using where clause check each employee salary which is greater than average value.Select that employees.Display the result using ToString() method.
Declare three variables- employee name, age, and salary
Create a list of employees using list collection.
Using Query compute the average salary of all the employees.
Using where clause check each employee salary which is greater than average value.
Select that employees.
Display the result using ToString() method.
Example:
C#
// C# program to display the lits of employees // whose salary is greater than average salary // of all the employeesusing System;using System.Collections.Generic;using System.Linq;  class Employee{  // Declare three variables// employee name, age, and Salarystring emp_Name;int emp_Age;int emp_Salary;  // Get the datapublic override string ToString(){    return " " + emp_Name + " " +                   emp_Age + " " + emp_Salary;}  // Driver codestatic void Main(string[] args){          // Create the list of 5 employees    List<Employee> emp = new List<Employee>()    {        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<Employee> 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
 
CSharp LINQ
CSharp-programs
Picked
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
Switch Statement in C#
Convert String to Character Array in C#
C# | How to insert an element in an Array?
Linked List Implementation in C#