本文介紹了LINQ 與 OR 連接的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想使用 OR 語句對 LINQ 進行 JOIN.
I want to do a JOIN with LINQ using an OR statement.
這是我開始的 SQL 查詢:
Here is the SQL query I'm starting with:
SELECT t.id
FROM Teams t
INNER JOIN Games g
ON (g.homeTeamId = t.id OR g.awayTeamId = t.id)
AND g.winningTeamId != 0
AND g.year = @year
GROUP BY t.id
我無法將該 ON 子句轉換為 LINQ.這就是我所在的位置:
I'm having trouble converting that ON clause to LINQ. This is where I'm at:
var y = from t in db.Teams
join g in db.Games on t.ID equals g.AwayTeamID //missing HomeTeamID join
where g.WinningTeamID != 0
&& g.Year == year
group t by t.ID into grouping
select grouping;
我想我可以使用:
join g in db.Games on 1 equals 1
where (t.ID == g.HomeTeamID || t.ID == g.AwayTeamID)
這有效,但似乎有點像hacky.有沒有更好的辦法?
and this works but seems kind of seems hacky. Is there a better way?
推薦答案
where 子句應用布爾條件,因此使用||"是要走的路.您可以鏈接多個 where 子句,但我相信這會給您一個和"操作,而不是或".
The where clause applies a boolean condition, so using "||" is the way to go. You can chain multiple where clauses but I believe that will give you a "and" operation, rather than an "or".
這篇關于LINQ 與 OR 連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!