Maharudra Gadekar

Maharudra Gadekar

  • 1.3k
  • 352
  • 77k

How to find out 2nd or nth highest salary?

Feb 27 2020 1:09 AM
Dear All,
 
 
I want to find out 2nd highest  or nth highest  salary using linq....
 
bellow is my tried code: 
 
List<Employee> employees = new List<Employee>()
{
new Employee { Id = 1, UserName = "Anil" , Salary = 5000},
new Employee { Id = 2, UserName = "Sunil" , Salary = 6000},
new Employee { Id = 3, UserName = "Lokesh" , Salary = 5500},
new Employee { Id = 4, UserName = "Vinay" , Salary = 7000},
new Employee { Id = 5, UserName = "Vijay" , Salary = 6000},
new Employee { Id = 6, UserName = "vikas" , Salary = 6000}
};
 
Linq Query: 
 
var result = employees.OrderByDescending(x => x.Salary).Select(x => x.Salary).Skip(1).Take(3 - 1).FirstOrDefault(); 
 
but return only one reocrd from this query.the actual result is return 3 records.
 
I am also try using tolist().
 
help me......
 
thanks in advance 
 
 
 

Answers (8)