使用linq查找第二个最高薪水

时间:2010-12-15 11:08:14

标签: c# linq

我有以下sql查询来查找第二个最高薪水。


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

我想将其转换为Linq语句。

6 个答案:

答案 0 :(得分:21)

我认为您要问的是找到薪水第二高的员工?

如果是这样,那就像

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

如果多名员工可能有相同的工资,并且您希望返回所有员工的IEnumerable,其薪水可以达到第二高的水平:

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

(感谢@ diceguyd30建议后一种增强)

答案 1 :(得分:2)

您可以将下面的比较器类定义为:

    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
    }

使用它如下:

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

或者没有同等比较(两行):

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

编辑:正如Ani所说,使用sql应该做:var lst = myDataContext.Employees.AsEnumerable();但如果是商业软件,最好使用TSQL或找到另一种linq方式。

答案 2 :(得分:2)

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();

答案 3 :(得分:0)

使用 LINQ ,您可以找到第三高薪:

// 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);

答案 4 :(得分:0)

这将适用于重复记录以及第n个最高薪水,只需要玩一下并跳过多数民众赞成以下所有用于ex的就是下面第3个最高薪水,并且表中存在重复记录-

emplist.OrderByDescending(x => x.Salary).Select(x=>x.Salary).Distinct().Take(3).Skip(2).First();

答案 5 :(得分:0)

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);
    }
}