I want to return a result set from a join on two tables. So far I've joined the table but am having difficulty figuring out the last part (or possibly the whole thing needs rewired).
var x = _context.GameHasPlayers
.Join(_context.Games,
ghp => ghp.GameId,
g => g.Id,
(gameHasPlayer, game) => /*What goes here*/ );
In SQL I'd simply write this:
select * from GameHasPlayer ghp
join Game g on g.Id = ghp.GameId
Let's say this should return 2 Games, each with 2 players, so a total of four rows.
It runs if I use in the last line of my c# query:
(gameHasPlayer, game) => new Game { });
But I just get four (as expected) new empty Game
classes, so I'm struggling with how I can return the entire result set (i.e. all 4 rows of both tables joined) - do I need to create some new model/view that combines Game
and GameHasPlayer
first?
(e.g. in SQL I could create a view that implements the above SQL query and return a generic dataset or recreate the view c# side?)
----- edit -----
I'm also trying below:
var x = from ghp in _context.GameHasPlayers
from g in _context.Games
where g.Id == ghp.GameId
select (g) // or select (ghp)
which gives me ACTUAL results but only for g
or ghp
- if I try select (g, ghp)
it just doesn't play ball!