如何使用LINQ查询从Employee表和Salary表中计算第五高薪

时间:2019-06-18 01:40:44

标签: linq

表1:EmolyeeTable

Eid  Ename

1    Jonh   
2    James
3    Raj
4    Tisan
5    Jack

表2:SalaryTable

Sid   Salary  Eid

1     10000    1

2     20000    2

3     30000    3

4     40000    4

5     50000    5

我想使用LINQ查询输出第五高的Ename和薪水。

O / P:

Ename  Salary

Jack   50000

2 个答案:

答案 0 :(得分:0)

您可以使用:

示例(您需要添加适当的检查):

    var q1 = from t1 in table1 join t2 in table2 on t1.EId equals t2.EId select new {t1.EName, t2.Salary};
    var q2 = q1.OrderByDescending(t=>t.Salary);
    var q3 = q2.Take(5).Select(t=>t.EName); //// In case you'd like to get Top-5 member names.
    var q4 = q2.ElementAt(4).EName //// In case you need only the 5th member name.

答案 1 :(得分:0)

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

要获得最高薪水,就可以。

<?php
$user = $_POST["PHP_AUTH_USER"];
$timestamp =date('d/m/Y H:i');
$text = "{$user} Logged in as: {$_SERVER['PHP_AUTH_USER']} at: {$timestamp} With IP: {$_SERVER['REMOTE_ADDR']\n";]}
$file = fopen("./users.txt","a+ \n");
fwrite($file, $text);
fclose($file);
?>

对于第五高的薪水,除了上述以外,您还可以使用“跳过”。例如,

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