linq - 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
解决方案
目前尚不清楚您是在寻找最高薪水还是第五高薪水。无论哪种情况,您都可以为此目的使用 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();
推荐阅读
- vba - 在打开的文件中添加书签并将它们保存为 dcox
- python - 为什么 subprocess 命令打印 0 而不是路径?
- c# - 如何在 C# 中比较 XmlDocument 的两个 XmlNode?
- c++ - 与 fstream::read 不一致的结果
- neo4j - 有没有办法针对 OR where 子句优化此查询?
- c# - 如何使用 C# 将 JSon 文件发送到远程 Web 服务器
- database - 目标表在开始运行 Kettle 脚本时被截断
- c# - 如何在c#中获取存储过程输出参数
- php - 从 Woocommerce 3.3 中的 Storefront 主题中删除面包屑
- php - 试图在php数组中获取非对象的属性