I have the following data:
Games
Id | AwayTeamId | HomeTeamId | SportId | foo | bar |
------------------------------------------------------------
1 | 1 | 3 | 1 | 42 | 23 |
2 | 2 | 3 | 1 | 81 | 25 |
3 | 3 | 1 | 1 | 40 | 23 |
Teams
Id | TeamName |
-----------------
1 | Lions |
2 | Tigers |
3 | Bears |
Sports
Id | SportName |
--------------------
1 | Soccer |
2 | Baseball |
3 | Basketball |
and I would like data like this for a Game
{
"id": 2,
"sport": {
"id": 1,
"name": "soccer"
}
"awayTeam": {
"id": 2,
"name": "Tigers"
},
"homeTeam": {
"id": 3,
"name": "Bears"
},
"foo": 81,
"bar": 25
}
Here's where I'm stuck: I know how to do a join for Games and Teams but don't know how to
- have
Game.AwayTeamIdandGame.HomeTeamIdboth join on the same column in theTeamstable - include the additional
Sportlookup
Obviously I'm looking to make a single database call if possible.
Here is my C# code that I know is incomplete:
var gameLookup = Context.Teams
.Join(
Context.Games.Where(b => b.Id == id),
team => team.Id,
game => game.AwayTeamId,
(team, game) => new
{
game.Id,
AwayTeam = team,
game.foo,
game.bar
}
).FirstOrDefault();
Thank you in advance.