我有兩個表:有什麼不對這種LINQ查詢
Team: teamId, teamName
Player: playerId, teamId, playerName
我想通過playerName的teamName。我寫了兩個querys,其中一個不起作用。
var query = from t in dc.Teams
where t.teamId == ((from p in dc.Players
where p.playerName == "kobe"
select p.teamId).SingleOrDefault())
select t.teamName; //Doesn't work
var query = from t in dc.Teams
join p in dc.Players
on t.teamId equals p.teamId
where p.playerName == "kobe"
select t.teamName; //Works
任何人都可以告訴我爲什麼第一個查詢無法工作?
您應該已經知道現在如何縮進代碼... – gdoron
錯誤消息?看起來你正在嘗試在SingleOrDefault()的調用之後進行投影(select)。這是沒有意義的。 – Marcote
您是否遇到錯誤,或者它只是沒有返回您期望的內容? –