在此T-SQL中,Linq to Enties的子查询中是否没有 [英] Is there a not in sub-query for Linq to Enties as in this T-SQL

查看:52
本文介绍了在此T-SQL中,Linq to Enties的子查询中是否没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Assume the classic self-referencing Employee table where each employee may have at most one ReportsTo, as created with this T-SQL snippet and sample data:

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的管理器

I can select the manager for Region 1 with this T-SQL

select * from Employees 
where Region = 'Region 1' and 
ReportsTo not in (select Id from Employees where Region = 'Region 1')

换句话说,经理是一名雇员,在其所在区域未向其报告.

In other words, the manager is an employee with not reports to in his region.

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

Now, how do I determine the manager for Region 1 using Linq?

推荐答案

类似这样的事情:

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;

这篇关于在此T-SQL中,Linq to Enties的子查询中是否没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆