C# Program to Print the Employees Whose Salary Is Greater Than the Average of All Employees Salaries Using LINQ

You are currently viewing C# Program to Print the Employees Whose Salary Is Greater Than the Average of All Employees Salaries Using LINQ

C# program to print the employees whose salary is greater than the average of all employees salaries using LINQ

// Program to print the employees whose salary is 
//greater than average salary using LINQ in C#.

using System;
using System.Collections.Generic;
using System.Linq;

public class Employee
{
    int ID;
    string Name;
    int Age;
    int Salary;

    public override string ToString()
    {
        return ID + " " + Name + " " + Age + " " + Salary;
    }

    static void Main(string[] args)
    {
        List<Employee> employees = new List<Employee>()
        {
             new Employee {ID=101,   Name="Sumit"    ,Age=23, Salary=4000},
             new Employee {ID=102,   Name="Kiran"    ,Age=24, Salary=6000},
             new Employee {ID=103,   Name="Suman"    ,Age=25, Salary=7000},
             new Employee {ID=104,   Name="Raman"    ,Age=26, Salary=9000},
        };

        IEnumerable<Employee> Query =
            from emp in employees
            let totSal=employees.Sum(sal=>sal.Salary)
            let avgSal = totSal/4
            where emp.Salary > avgSal
            select emp;


        Console.WriteLine("ID  Name  Age Salary");
        Console.WriteLine("=====================");
        foreach (Employee s in Query)
        {
            Console.WriteLine(s.ToString());
        }
        Console.WriteLine("=====================");
    }
}

Output

ID  Name  Age Salary
=====================
103 Suman 25 7000
104 Raman 26 9000
=====================
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply