我在linq中使用any时遇到问题,我不知道如何正确执行。 我必须用linq编写:
SELECT ename, job, deptno
FROM emp
WHERE sal > ANY
(
SELECT DISTINCT sal
FROM emp
WHERE deptno = 30
);
我只写这个:
var min = (from emp in Emps
where emp.Deptno == 30
select emp.Sal
).Distinct();
var result = (from emp in Emps
where min.Any() > emp.Sal
select new
{
emp.Ename
});
答案 0 :(得分:1)
Linq没有像Sql Server一样的any/some运算符。
var salariesInTargetDepartment = Emps
.Where(x => x.Deptno == 30)
.Select(x => x.Sal)
.Distinct()
.ToList(); // the ToList is not required, but seeing you're going to be executing
// against this query many times, it will be better to cache the results.
var matchingEmployees = Emps
.Where(emp => salariesInTargetDepartment
.Any(target => emp.Sal > target)
);
第二条语句中的where
子句说:“仅当该记录的Sal
属性大于salariesInTargetDepartment
集合中的至少一个条目时,才包括该记录。”