2013-06-27 38 views
-2
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 

限制:不允許包含()。如何將這個SQL轉換爲LINQ? (涉及的內連接和子查詢)

+0

可能重複[轉換SQL到LINQ查詢](http://stackoverflow.com/questions/8988531/convert-sql-to-linq-query) –

回答

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 
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() 

BT的方式,你的SQL子查詢是多餘的,你可以select esr.* from Schedules sch