Showing posts with label linq. Show all posts
Showing posts with label linq. Show all posts

Thursday, November 25, 2021

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


var Customer = Customers.OrderByDescending(x => x.salary)

.Select(x => x.salary).Distinct().Take(NthNumber_Salary)

.Skip(NthNumber_Salary - 1).FirstOrDefault();  

 Console.Write("3rd Highest Salary is: " + ee);

Difference Between First() And FirstOrDefault()

The major difference between First and FirstOrDefault is that First() will throw an exception if there is no result data for the supplied criteria whereas FirstOrDefault() will return the default value (null) if there is no result data.

 
First() will throw an exception if there is no result data, as you can see below.
 
Difference between First() and FirstOrDefault()
 FirstOrDefault() returns the default value (null) if there is no result data and you can see the result in the following screen.
 
Difference between First() and FirstOrDefault()