[英]Is there a not in sub-query for Linq to Enties as in this T-SQL
假設經典的自引用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'))
我可以使用此T-SQL為區域1選擇管理器
select * from Employees
where Region = 'Region 1' and
ReportsTo not in (select Id from Employees where Region = 'Region 1')
換句話說,經理是一名在其所在地區沒有報告的員工。
現在,如何使用Linq確定1區的管理器?
這樣的事情怎么樣:
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;
我相信你可以通過Linq在查詢中查詢。 它看起來像下面
db.Employees.Where(u => u.Region = 'Whatever').Where(u =>
db.Employees
.Where(v => ReportsTo <> 'Whatever')
.Select(v => v.Id)
.Contains(u.Id)
)
這可能需要一些工作才能准備好,但我認為這是一般的想法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.