我正在学习LINQ,我希望使用LINQ找到第三个最高工资。我知道如何查询集合,但我无法弄清楚如何解决这个问题
void Main()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee { Id = 1, Name = "John Doe", Salary = 1500 });
employees.Add(new Employee { Id = 1, Name = "John Wow", Salary = 10000 });
employees.Add(new Employee { Id = 1, Name = "Bill", Salary = 1000 });
employees.Add(new Employee { Id = 1, Name = "Jane Doe", Salary = 2000 });
var secondHighestSalary=from e in employees
select e ----
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public double Salary { get; set; }
}
答案 0 :(得分:4)
您可以尝试以下代码段
List<Employee> employees = new List<Employee>();
employees.Add(new Employee { Id = 1, Name = "John Doe", Salary = 1500 });
employees.Add(new Employee { Id = 1, Name = "John Wow", Salary = 1800 });
employees.Add(new Employee { Id = 1, Name = "Bill", Salary = 1000 });
employees.Add(new Employee { Id = 1, Name = "Jane Doe", Salary = 2000 });
var secondHighestSalary=employees.OrderByDescending(e => e.Salary)
.Skip(2)
.Take(1);
secondHighestSalary.Dump();