我是LINQ的新手,正在尝试将SQL子查询转换为lINQ。我们可以在LINQ中编写像SQL这样的子查询吗?
这是数据库
Table EMP
Name Null? Type
EMPNO NOT NULL NUMBER(4)
NAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
Table Dpt
Name Null? Type
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
这是SQL查询
SELECT *
FROM emp
WHERE deptno IN (SELECT deptno
FROM dept
WHERE dname = 'RESEARCH');
这是我的努力:
var r = (from query in conn.EMPs
where (query.DEPTNO == (from q in conn.DEPTs
where q.DNAME == "RESERCH"
select q.DEPTNO))
select new
{
query
}).ToList();
答案 0 :(得分:0)
我喜欢使用联接:
var r = (from query in conn.EMPs
join q in conn.DEPTs on query.deptno equals q.DEPTNO
select new { query = query, q = q})
.Where(x => x.q.DNAME == "RESEARCH")
.ToList();
答案 1 :(得分:0)
var departmentNos = dept.Where(dpt => dpt.dname =='RESEARCH')。Select(dptNo => dptNo.deptno);
var employee = emp.Where(e => departmentNos.Contains(e.deptno));
员工才是最终结果。