Ask Your Question
3

What is the LINQ query to retrieve the employee with the second highest salary?

asked 2023-01-04 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-11-11 12:00:00 +0000

scrum gravatar image

Assuming we have a list of employees with properties "Name" and "Salary", the LINQ query to retrieve the employee with the second highest salary would be:

var secondHighestSalaryEmployee = employees.OrderByDescending(e => e.Salary).Skip(1).FirstOrDefault();

This query first sorts the employees in descending order by their salary, then skips the first employee (i.e. the one with the highest salary), and finally selects the first employee from the remaining list (i.e. the one with the second highest salary).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-04 11:00:00 +0000

Seen: 10 times

Last updated: Nov 11 '22