使用 LINQ 打印工资高于所有员工平均工资的 C# 程序
LINQ 被称为语言集成查询,它是在 .NET 3.5 中引入的。它使 .NET 语言能够生成查询以从数据源检索数据。它消除了编程语言和数据库之间的不匹配,并且无论使用哪种类型的数据源,用于创建查询的语法都是相同的。在本文中,我们将学习如何显示工资高于所有员工工资平均值的员工详细信息列表。
例子:
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}
方法:
- 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.
例子:
C#
// C# program to display the lits 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());
}
}
}
输出:
Employee-Name Employee-Age Employee-Salary
sravan 21 26199
harsha 23 25000
deepu 23 23456