;

# Find 2nd ,3rd or Nth Highest salary using LINQ

###### Tutorialsrack 21/06/2019 C# LINQ

In this article, we will learn how to find 2nd, 3rd or Nth highest salary of an employee using LINQ. It is the most common question asked by an interviewer to find Nth highest salary of an employee using LINQ.

Find the 2nd or 3rd or Nth highest salary using SQL

So, in this article, we will see how to find it using LINQ.

First Let us declare an “Employee” class.

##### Code - Declare Employee Class
``````class Employee
{
public int empid { get; set; }
public string empname { get; set; }
public double salary { get; set; }
}``````

Then after we populate Employees record to that class.

##### Code - Populate the Employee Record
``````//Populate the Employee Record
public static List GetEmployeeRecord()
{
List empList = new List();
empList.Add(new Employee { empid = 1, empname = "Employee1", salary = 10000 });
empList.Add(new Employee { empid = 2, empname = "Employee2", salary = 8500 });
empList.Add(new Employee { empid = 3, empname = "Employee3", salary = 14200 });
empList.Add(new Employee { empid = 4, empname = "Employee4", salary = 15000 });
empList.Add(new Employee { empid = 5, empname = "Employee5", salary = 10000 });
empList.Add(new Employee { empid = 6, empname = "Employee6", salary = 6000 });
empList.Add(new Employee { empid = 7, empname = "Employee7", salary = 20000 });
empList.Add(new Employee { empid = 8, empname = "Employee8", salary = 15000 });
empList.Add(new Employee { empid = 9, empname = "Employee9", salary = 7000 });
empList.Add(new Employee { empid = 10, empname = "Employee10", salary = 7000 });
return empList;
}``````

Then, use this method to find the 2nd, 3rd or Nth highest salary of an employee.

##### Code - Use this Method
``````        //Highest salary Using Method Syntax in LINQ
public static void Nthhighestsalary()
{
var empList = GetEmployeeRecord();
var ee = empList.OrderByDescending(x => x.salary)
.Select(x => x.salary).Distinct().Take(NthNumber_Salary)
.Skip(NthNumber_Salary - 1).FirstOrDefault();
Console.Write("3rd Highest Salary is: " + ee);
}``````

Here is the Complete program to find the 2nd, 3rd, or Nth Highest Salary of an Employee using LINQ.

##### Complete Program Code
``````using System;
using System.Collections.Generic;
using System.Linq;

namespace HighestSalaryUsingLINQ
{

class Employee
{
public int empid { get; set; }
public string empname { get; set; }
public double salary { get; set; }

}
class Program
{
//Populate the Employee Record
public static List GetEmployeeRecord()
{
List empList = new List();
empList.Add(new Employee { empid = 1, empname = "Employee1", salary = 10000 });
empList.Add(new Employee { empid = 2, empname = "Employee2", salary = 8500 });
empList.Add(new Employee { empid = 3, empname = "Employee3", salary = 14200 });
empList.Add(new Employee { empid = 4, empname = "Employee4", salary = 15000 });
empList.Add(new Employee { empid = 5, empname = "Employee5", salary = 10000 });
empList.Add(new Employee { empid = 6, empname = "Employee6", salary = 6000 });
empList.Add(new Employee { empid = 7, empname = "Employee7", salary = 20000 });
empList.Add(new Employee { empid = 8, empname = "Employee8", salary = 15000 });
empList.Add(new Employee { empid = 9, empname = "Employee9", salary = 7000 });
empList.Add(new Employee { empid = 10, empname = "Employee10", salary = 7000 });
return empList;
}

//Change the No. to N number.
//For Example, 1,2,3...or which No. of salary you want to find.
static int NthNumber_Salary = 4;
static void Main(string[] args)
{
Nthhighestsalary();
}

//Highest salary Using Method Syntax in LINQ
public static void Nthhighestsalary()
{
var empList = GetEmployeeRecord();
var ee = empList.OrderByDescending(x => x.salary)
.Select(x => x.salary).Distinct().Take(NthNumber_Salary)
.Skip(NthNumber_Salary - 1).FirstOrDefault();
Console.Write("3rd Highest Salary is: " + ee);
}
}
}
``````