I am trying to create a view for a many-to-many ViewModel GameGenre but I am unsure how to design the mvc to select all games and group by games but show all genres for each game.
I was looking how to achieve my purpose through LINQ and group-by but most answers that I found were using it for one model.
Also, I do not feel confident with the view model that I have right now:
public class GameStoreViewModel
{
[Key]
public Guid GameId { get;}
public string GameName { get;}
public Genre Genre { get; set; }
public IEnumerable<Genre> GenreList { get; set; }
}
These are my models:
public class Game
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class Genre
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class GameGenre
{
public Guid Id { get; set; }
public Guid GameId { get; set; }
public Guid GenreId { get; set; }
public Game Game { get; set; }
public Genre Genre { get; set; }
}
The basic query to run on SQL is:
SELECT ga.Id, ga.Name, ge.Name FROM Game ga
LEFT JOIN GameGenre gg ON ga.Id = gg.GameId
LEFT JOIN Genre ge ON ge.Id = gg.GenreId;
The result
But that results in games being repeated multiple times which will not look good for a front store. So, I would like to make the view to get each game with all its genres like
I am more concerned about the view model and the controller code than the html view.
Apologies if my explanation is not clear, please let me know. I appreciate any assistance and thank you in advance!