是否存在Linq to Enties的子查询,就像在此T-SQL中一样

Ral*_*ton 3 linq linq-to-entities

假设经典的自引用Employee表,其中每个员工最多可以有一个ReportsTo,如使用此T-SQL片段和样本数据创建的:

create table Employees
(
    Id          int identity primary key,
    Name        nvarchar(30),
    Region      nvarchar(10),
    ReportsTo   int null
        foreign key(ReportsTo) references Employees(Id)
)

insert into Employees values('Boss','HO',null)
insert into Employees values('Underling', 'HO',
        (select Id from Employees where Name='Boss'))
insert into Employees values('Self Important', 'Region 1',
        (select Id from Employees where Name='Underling'))
insert into Employees values('Very Underling', 'Region 1',
        (select Id from Employees where Name='Self Important'))
Run Code Online (Sandbox Code Playgroud)

我可以使用此T-SQL为区域1选择管理器

select * from Employees 
where Region = 'Region 1' and 
ReportsTo not in (select Id from Employees where Region = 'Region 1')
Run Code Online (Sandbox Code Playgroud)

换句话说,经理是一名在其所在地区没有报告的员工.

现在,如何使用Linq确定1区的管理器?

AJ.*_*AJ. 6

这样的事情怎么样:

from e in context.Employee
where e.Region == "Region 1" 
&& !(from e2 in context.Employee
     where e2.Region == "Region 1"
     select e2.Id).ToList().Contains(e.ReportsTo)
select e;
Run Code Online (Sandbox Code Playgroud)