首页 > 解决方案 > How to calculate the 5th heighest salary from Employee Table and Salary Table using LINQ query

问题描述

Table1: EmolyeeTable

Eid  Ename

1    Jonh   
2    James
3    Raj
4    Tisan
5    Jack

Table2: SalaryTable

Sid   Salary  Eid

1     10000    1

2     20000    2

3     30000    3

4     40000    4

5     50000    5

I want to output the 5th heighest Ename and salary using a LINQ query.

O/P:

Ename  Salary

Jack   50000

标签: linq

解决方案


目前尚不清楚您是在寻找最高薪水还是第五高薪水。无论哪种情况,您都可以为此目的使用 Join 和 OrderBy。

对于最高薪水,你可以做到。

var result = employees.Join(salaries,e=>e.Eid,s=>s.Eid,(e,s)=> new {Name=e.EName,Salary=s.Salary})
                          .OrderByDescending(x=>x.Salary).First();

对于第 5 高的薪水,除了上述之外,您还可以使用 Skip。例如,

var result = employees.Join(salaries,e=>e.Eid,s=>s.Eid,(e,s)=> new {Name=e.EName,Salary=s.Salary})
                          .OrderByDescending(x=>x.Salary).Skip(4).First();

推荐阅读