LINQ中的子查询帮助

本文关键字:查询 帮助 LINQ | 更新日期: 2023-09-27 18:00:07

嗨,有人能帮我如何在LINQ中编写以下查询吗?因为我是LINQ 的新手

select * from Employee where employeeid = 'E101'
or empdept = (select dept from departments where deptid = 'D101')

提前感谢

LINQ中的子查询帮助

您可以使用群组加入

from e in employee
join d in deparment.Where(x => x.deptid == "D101") 
    on e.empdept equals d.dept into g
where e.employeeid == "E101" || g.Any()
select e;

生成以下SQL

DECLARE @p0 NVarChar(1000) = 'E101'
DECLARE @p1 NVarChar(1000) = 'D101'
SELECT *
FROM [employee] AS [t0]
WHERE ([t0].[employeeid] = @p0) OR (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [deparment] AS [t1]
    WHERE ([t0].[empdept] = [t1].[dept]) AND ([t1].[deptid] = @p1)
    ))