Q:

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

belongs to collection: C# LINQ Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to print employees whose salary is greater than average salary using LINQ in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

// 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 . . .

Explanation:

In the above program, we created an Employee class that contains data members IDNameAge, and Salary. Here we override the ToString() method to return Employee information. The Employee class also one more method is known as Main().

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

In the Main() method, we created List collection for Employees, It contains information of 4  employees.

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

In the above code, we created a query to get a list of employees whose salary is greater than the average of all employee's salaries using let statements of LINQ. Then we printed the Employee detail using the "foreach" loop on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# LINQ Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to print only those numbers whose value... >>
<< C# program to print the names that contain \'...