2012-07-18 267 views
2

我在TSQL以下查詢多加入實體框架

select * from users 
inner join linkUserPhoneNumber on users.UserId = linkUserPhoneNumber.UserId 
INNER JOIN PhoneNumber ON PhoneNumber.PhoneNumberId = 
    linkUserPhoneNumber.PhoneNumberId 
    where UserName = 'superuser' and password ='password' 

我在實體框架下面的查詢

var query = (from u in myEntities.Users 
    join link in myEntities.linkUserPhoneNumbers on u.UserId equals link.UserId 
    join p in myEntities.PhoneNumbers on p.PhoneNumberId equals link.PhoneNumberId 
    where u.UserName == Username && u.Password == Password 
    select u).ToList(); 

當我嘗試編譯它,我得到

錯誤3名稱'p'不在'等於'左側的範圍內。考慮交換'equals'兩邊的表達式。
錯誤4名稱'鏈接'不在'等於'右側的範圍內。考慮交換'equals'兩邊的表達式。

+0

我想你複製粘貼,這樣我就可以看到一個 'eqals'這可能是'等於' – Amedio 2012-07-18 09:03:00

回答

5

p.PhoneNumberId equals link.PhoneNumberId 

應該是究竟是什麼錯誤是說

link.PhoneNumberId equals p.PhoneNumberId 

的完整代碼

var query = (from u in myEntities.Users 
    join link in myEntities.linkUserPhoneNumbers on u.UserId equals link.UserId 
    join p in myEntities.PhoneNumbers on link.PhoneNumberId equals p.PhoneNumberId 
    where u.UserName == Username && u.Password == Password 
    select u).ToList(); 
+0

謝謝,這很簡單。我想我一直在做TSQL很長一段時間,我沒有注意到。再次感謝 – 2012-07-18 09:17:04