Showing posts with label Finding Nth large Salary from List. using Lambda expression/Linq and SQL Server.. Show all posts
Showing posts with label Finding Nth large Salary from List. using Lambda expression/Linq and SQL Server.. Show all posts

Saturday, July 20, 2019

Console Program for finding Nth Large Salary from List. using Lambda expression/Linq.



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace DotNetByPriyanshu_App

{

    class Program

    {

        static void Main(string[] args)

        {

            List<TestClass> testClasses = new List<TestClass>();

            for (int i = 0; i < 10; i++)

            {

                TestClass testClass = new TestClass();

                testClass.id = 1 + i;

                testClass.EmpName = "Dot Net By Priyanshu" + i;

                testClass.salary = 20000 + i;

                testClasses.Add(testClass);

            }


             int N = 2; //we can change value according to requirement.


            // It will return 2nd Heigest salary.

            var Nth_HieghtSal = testClasses.OrderByDescending(e => e.salary).Skip(N-1).First();


          //Note : if you want to find n salary then you can simply change the value of N 

        }
    }

    public class TestClass

    {

        public int id { get; set; }

        public string EmpName { get; set; }

        public int salary { get; set; }

}

}

Monday, July 2, 2018

SQL Query For Finding nth Heighest & Lowest Salary

SQL Query For Finding nth Heighest Salary

SELECT Salary,EmpName
FROM
  (
   SELECT Salary,EmpName,ROW_NUMBER() OVER(ORDER BY Salary Desc) As RowNum
   FROM EMPLOYEE
   ) As A
WHERE A.RowNum = 3;

SQL Query For Finding nth LowestSalary

SELECT Salary,EmpName
FROM
  (
   SELECT Salary,EmpName,ROW_NUMBER() OVER(ORDER BY Salary ASC) As RowNum
   FROM EMPLOYEE
   ) As A
WHERE A.RowNum = 2;