Saturday, 20 July 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; }

}

}

1 comment:

  1. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.

    Best PHP Training Institute in Chennai|PHP Course in chennai
    Best .Net Training Institute in Chennai
    Dotnet Training in Chennai
    Dotnet Training in Chennai

    ReplyDelete