このT-SQLスニペットとサンプルデータで作成された、各従業員が最大で1つのReportsToを持つことができる従来の自己参照Employeeテーブルを想定します:このT-SQLのようにLinq to Entiesのサブクエリにはない
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
select * from Employees
where Region = 'Region 1' and
ReportsTo not in (select Id from Employees where Region = 'Region 1')
でリージョン1のマネージャーを選択することができ、管理者は、彼の地域内にありません報告と従業員です。
ここで、Linqを使用して地域1のマネージャを決定する方法を教えてください。
おかげ---私はあなたがIntとして選択e2.Idをキャストする必要があると思いますか?これはe.ReportsToのタイプです。そのマイナーな編集以外は、そのトリックをするようです。 –
あなたの質問が間違っていない限り、キャストがなぜ必要なのか分かりません。 –
実際には '.ToList()'を使う必要はありませんでした。それは私にエラーを与えていました。それを取り除くことは、魅力=のように私のために働く) –