我正在尝试在linq中编写一个子查询,这是我的SQL脚本
Select id, name from employee where id in (select dictinct id in salaryEmp)
我尝试了几种组合但没有获得所需的输出。我将非常感谢这方面的任何帮助
答案 0 :(得分:0)
这应该这样做:
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 });
答案 1 :(得分:0)
你没有说出你的上下文是怎样的,但它会是这样的:
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 }