C#程序使用LINQ检查所有员工的薪水是否低于10000
给定员工的数据,现在我们的任务是检查是否所有员工的工资都小于 10000。所以我们使用 LINQ 的 All() 方法。此方法用于检查源序列中的所有元素是否满足给定条件。如果给定序列中存在的所有元素都通过了测试,它将返回 true。否则,它将返回 false。因此,为了解决给定的问题,我们使用以下 LINQ 查询:
result = Geeks.All(geek => geek.Emp_Salary < 10000);
这里, result是一个布尔类型变量,用于存储最终结果, Geeks是序列, All()方法用于查找工资低于 10000 的员工列表。
例子:
Input: {id = 301, Name = Mohit, Salary = 10000}
{id = 302, Name = Priya, Salary = 20000}
{id = 303, Name = Sohan, Salary = 40000}
{id = 304, Name = Rohit, Salary = 10000}
Output: False
Input: {id = 401, Name = Rohan, Salary = 1000}
{id = 404, Name = Mohan, Salary = 4000}
Output: True
示例 1:
C#
// C# program to determine the salary of all employees
// is less than 10000
using System;
using System.Linq;
using System.Collections.Generic;
class Geek{
#pragma warning disable 169, 414
int emp_id;
string Emp_Name;
int Emp_Salary;
string Emp_Department;
static void Main(string[] args)
{
// List of employee details
List Geeks = new List()
{
new Geek{emp_id = 401, Emp_Name = "Rajat", Emp_Salary = 50000},
new Geek{emp_id = 402, Emp_Name = "Ram", Emp_Salary = 65000},
new Geek{emp_id = 403, Emp_Name = "Krishna", Emp_Salary = 45000},
new Geek{emp_id = 404, Emp_Name = "Sonial", Emp_Salary = 20000},
new Geek{emp_id = 405, Emp_Name = "Mickey", Emp_Salary = 70000},
new Geek{emp_id = 406, Emp_Name = "Kunti", Emp_Salary = 50000},
};
bool result;
// Checking the salary of all employees
// is less than 10000
result = Geeks.All(geek => geek.Emp_Salary < 10000);
// Display result
if (result)
{
Console.Write("All the salaries are less than 10000");
}
else
{
Console.Write("All the salaries are not less than 10000");
}
}
}
C#
// C# program to determine the salary of all employees
// is less than 10000
using System;
using System.Linq;
using System.Collections.Generic;
class Geek{
#pragma warning disable 169, 414
int emp_id;
string Emp_Name;
int Emp_Salary;
string Emp_Department;
static void Main(string[] args)
{
// List of employee details
List Geeks = new List()
{
new Geek{emp_id = 501, Emp_Name = "Rohan", Emp_Salary = 3000},
new Geek{emp_id = 502, Emp_Name = "Mohan", Emp_Salary = 3000},
new Geek{emp_id = 503, Emp_Name = "Sham", Emp_Salary = 4000},
new Geek{emp_id = 504, Emp_Name = "Sonial", Emp_Salary = 1000},
};
bool result;
// Checking the salary of all employees
// is less than 10000
result = Geeks.All(geek => geek.Emp_Salary < 10000);
// Display the result
Console.WriteLine("Is the salary of the Geek's employees is < 10000: " + result);
}
}
输出
All the salaries are not less than 10000
示例 2:
C#
// C# program to determine the salary of all employees
// is less than 10000
using System;
using System.Linq;
using System.Collections.Generic;
class Geek{
#pragma warning disable 169, 414
int emp_id;
string Emp_Name;
int Emp_Salary;
string Emp_Department;
static void Main(string[] args)
{
// List of employee details
List Geeks = new List()
{
new Geek{emp_id = 501, Emp_Name = "Rohan", Emp_Salary = 3000},
new Geek{emp_id = 502, Emp_Name = "Mohan", Emp_Salary = 3000},
new Geek{emp_id = 503, Emp_Name = "Sham", Emp_Salary = 4000},
new Geek{emp_id = 504, Emp_Name = "Sonial", Emp_Salary = 1000},
};
bool result;
// Checking the salary of all employees
// is less than 10000
result = Geeks.All(geek => geek.Emp_Salary < 10000);
// Display the result
Console.WriteLine("Is the salary of the Geek's employees is < 10000: " + result);
}
}
输出
Is the salary of the Geek's employees is < 10000: True