LINQ join with OR

前端 未结 2 423
清酒与你
清酒与你 2021-01-17 14:35

I want to do a JOIN with LINQ using an OR statement.

Here is the SQL query I\'m starting with:

SELECT  t.id
FROM Teams t
INNER JOIN Games g 
   ON (g         


        
相关标签:
2条回答
  • 2021-01-17 15:21

    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".

    0 讨论(0)
  • 2021-01-17 15:25

    I struggled with this as well until I found the following solution, which worked well for my situation:

    var y = from t in db.Teams
            from g in db.Games
            where
            (
                t.ID == g.AwayTeamID
                || t.ID == g.HomeTeamID
            )
               && g.WinningTeamID != 0
               && g.Year == year
            group t by t.ID into grouping
            select grouping;
    

    Under the covers, your solution probably works very close to this one. However, I bet this one is just a bit faster if you benchmark it since it is not JOINING every item in the first dataset with every item in the second dataset, which could be a disaster if either (or both) dataset were really big.

    0 讨论(0)
提交回复
热议问题