如何将此SQL转换为LINQ? (涉及的内部连接和子查询)

时间:2013-06-27 19:50:24

标签: c# sql sql-server tsql

select * from EmployeeScheduleRoles esr 
inner join
(
    select esr.ScheduleID from Schedules sch 
    inner join EmployeeScheduleRoles esr on esr.ScheduleID = sch.ID 
    inner join Employees emp on emp.ID = esr.EmployeeID
    where emp.ID = 15921
) subqry on subqry.ScheduleID = esr.ScheduleID 

限制:否'包含()'允许的。

2 个答案:

答案 0 :(得分:1)

此LINQ查询等同于您的SQL查询(对我来说似乎不是非常优化)

from esr in db.EmployeeScheduleRoles
join subqry in
  (from sch in db.Schedules
   join esr2 in db.EmployeeScheduleRoles on sch.ID equals esr2.ScheduleID
   join emp in db.Employees on esr2.EmployeeID equals emp.ID
   where emp.ID == 15921
   select esr2.ScheduleID)
on esr.ScheduleID equals subqry
select esr

答案 1 :(得分:0)

var yours = (from esr in EmployeeScheduleRoles
join sch in Schedules on esr.ScheduleID equals sch.ID 
join emp in Employees  on emp.ID equals esr.EmployeeID
where emp.ID == 15921
select esr).toList()

顺便说一句,你的sql子查询是多余的,你可以select esr.* from Schedules sch