Find 2nd max salary using linq

前端 未结 6 511
执笔经年
执笔经年 2021-01-02 07:47

I have following sql query for finding 2nd max salary.


Select * From Employee E1 Where
    (2) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
               


        
相关标签:
6条回答
  • 2021-01-02 08:10

    This will work for duplicate record as well as nth highest salary just need to play with take and skip thats all for ex below is for 3 rd highest salary with duplicate record present in table-

    emplist.OrderByDescending(x => x.Salary).Select(x=>x.Salary).Distinct().Take(3).Skip(2).First();
    
    0 讨论(0)
  • 2021-01-02 08:10
    public class Program
    {
        public static void Main()
        {
            IList<int> intList = new List<int>() { 10, 21, 91, 30, 91, 45, 51, 87, 87 };
    
            var largest = intList.Max();
    
            Console.WriteLine("Largest Element: {0}", largest);
    
            var secondLargest = intList.Max(i => {
                if(i != largest)
                    return i;
                return 0;
            });
    
            Console.WriteLine("second highest element in list: {0}", secondLargest);
        }
    }
    
    0 讨论(0)
  • 2021-01-02 08:11

    Using LINQ, you can find the 3rd highest salary like this:

    // first use LINQ to sort by salary, then skip first 2 and get next
    var thirdHighestSalary= (from n in db.Employee order by n.salary descending select n).distinct().skip(2).  FirstOrDefault()
    // write the result to console
    Console.WriteLine(Third Highest Salary is : {0},thirdHighestSalary.Salary);
    
    0 讨论(0)
  • 2021-01-02 08:15

    You can define equally comparer class as bellow:

        public class EqualityComparer : IEqualityComparer<Employee >
        {
            #region IEqualityComparer<Employee> Members
            bool IEqualityComparer<Employee>.Equals(Employee x, Employee y)
            {
                // Check whether the compared objects reference the same data.
                if (Object.ReferenceEquals(x, y))
                    return true;
    
                // Check whether any of the compared objects is null.
                if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                    return false;
    
                return x.Salary == y.Salary;
            }
    
            int IEqualityComparer<Employee>.GetHashCode(Employee obj)
            {
                return obj.Salary.GetHashCode();
            }
            #endregion
        }
    

    and use it as bellow:

        var outval = lst.OrderByDescending(p => p.Id)
                      .Distinct(new EqualityComparer()).Skip(1).First();
    

    or do it without equally comparer (in two line):

            var lst2 = lst.OrderByDescending(p => p.Id).Skip(1);
            var result = lst2.SkipWhile(p => p.Salary == lst2.First().Salary).First();
    

    Edit: As Ani said to work with sql should do : var lst = myDataContext.Employees.AsEnumerable(); but if is for commercial software it's better to use TSQL or find another linq way.

    0 讨论(0)
  • 2021-01-02 08:20
    List<Employee> employees = new List<Employee>()
    {
        new Employee { Id = 1, UserName = "Anil" , Salary = 5000},
        new Employee { Id = 2, UserName = "Sunil" , Salary = 6000},
        new Employee { Id = 3, UserName = "Lokesh" , Salary = 5500},
        new Employee { Id = 4, UserName = "Vinay" , Salary = 7000}
    };
    
    var emp = employees.OrderByDescending(x => x.Salary).Skip(1).FirstOrDefault();
    
    0 讨论(0)
  • 2021-01-02 08:27

    I think what you're asking is to find the employee with the second-highest salary?

    If so, that would be something like

    var employee = Employees
        .OrderByDescending(e => e.Salary)
        .Skip(1)
        .First();
    

    If multiple employees may have equal salary and you wish to return an IEnumerable of all the employees with the second-highest salary you could do:

    var employees = Employees
        .GroupBy(e => e.Salary)
        .OrderByDescending(g => g.Key)
        .Skip(1)
        .First();
    

    (kudos to @diceguyd30 for suggesting this latter enhancement)

    0 讨论(0)
提交回复
热议问题