简体   繁体   English

从SQL获取Linq查询

[英]Getting Linq query from the SQL

I am trying to get the Linq query from the SQL query. 我正在尝试从SQL查询中获取Linq查询。 The sql query is: sql查询是:

Select * from EMP where Salary = (Select Max(Salary) from EMP)

I have created a Linq query, but it doesn't work at all: 我创建了一个Linq查询,但是根本无法使用:

var result = emps.where(emp => emp.Salary == emps.Max(emp.sal));

How should I solve this problem? 我应该如何解决这个问题?

Try this: 尝试这个:

var maxsal = emps.Max(emp => emp.sal);
var result = emps.Where(e => e.sal == maxsal);

Assuming that you want to do it as a single linq query, I think this will do what you're after: 假设您希望将其作为单个linq查询进行操作,我认为这将满足您的需求:

var result = emps
    .Select(e => new { Emp = e, Max = emps.Max(_ => _.Salary)})
    .Where(e => e.Max == e.Emp.Salary)
    .Select(e => e.Emp);

** Update ** You can actually do that in the single statement: **更新**您实际上可以在单个语句中执行此操作:

var result = emps
    .Where(e => emps.Max(_ => _.Salary) == e.Salary);

Was there something else wrong with your original scenario? 您的原始场景还有其他问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM