简体   繁体   English

SQL TO LINQ哪里有Max子句

[英]SQL TO LINQ Where In Clause with Max

I want to convert the following query to LINQ equivalent. 我想将以下查询转换为LINQ等效项。 I've gone through this but unable to acheive my results which has Max. 我已经经历了这个,但是无法获得Max的结果。 can anyone help me in this regard. 谁能在这方面帮助我。

Below is my SQL Query: 以下是我的SQL查询:

select max(a.amount_limit) as AmountLimit from tbl_AmountEmpRole a where a.Role_Name in(select b.Role_name from tbl_UserRoles b where Emp_id=4) 

Thanks in Advance 提前致谢

You can try somthing like this : 您可以尝试这样的事情:

 var role_names=tbl_UserRoles.Where(x=> x.Emp_id==4).Select(x=>x.Role_name);
 var MaxacountLimit=tbl_AmountEmpRole.Where(y=>role_names.contains(y))
 .Max(z=>z.amount_limit);

Assuming a variable called context , this should work: 假设有一个名为context的变量,它应该可以工作:

context.tbl_AmountEmpRole
.Where(er => context.tbl_UserRoles.Where(ur => ur.Emp_id == 4).Select(ur => ur.Role_Name).Contains(er.Role_Name))
.Max(er => er.amount_limit);

However, I believe you might be able to rewrite your query using a JOIN : 但是,我相信您可以使用JOIN重写查询:

SELECT MAX(a.amount_limit) as AmountLimit FROM tbl_AmountEmpRole a 
INNER JOIN tbl_UserRoles b 
ON a.Role_Name = b.Role_Name
WHERE b.Emp_id = 4

In which case you could use the Join method: 在这种情况下,您可以使用Join方法:

context.tbl_AmountEmpRole.Join(context.tbl_UserRoles, er => er.Role_Name, ur => ur.Role_Name, new { er, ur })
.Where(j => j.ur.Emp_id = 4)
.Max(j => j.er.amount_limit);

Make sure to double-check syntax and signatures. 确保仔细检查语法和签名。 I wrote this from memory. 我是从记忆中写出来的。

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

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