我正在尝试在linq中编写子查询,这是我的SQL脚本
Select id, name from employee where id in (select dictinct id in salaryEmp)
我已经尝试了几种组合,但没有得到所需的输出。我将不胜感激这方面的任何帮助
这应该可以做到:
db.employee
.Where(x => salaryEmp.Select(x => x.id).Distinct().Contains(x.id))
.Select(x => new { x.id, x.name });
或者更好的是,你可以做:
var idList = salaryEmp.Select(x => x.id).Distinct().ToList();
var result = db.employee
.Where(x => idList.Contains(x.id))
.Select(x => new { x.id, x.name });
你没有说你的上下文是什么样子的,但它会是这样的:
from e in ctx.employee
where
(from s in ctx.salaryEmp
select s.Id).Distinct().Contains(e.id)
select new { e.Id, e.Name }
或者将子查询作为基于方法的查询:
from e in ctx.employee
where ctx.salaryEmp.Select(s => s.Id).Distinct().Contains(e.id)
select new { e.Id, e.Name }