I have a Game model that have many Organizer models attached to it. Here are my models:
public class Organizer
{
public int ID { get; set; }
public string Name { get; set; }
public List<Game> Games {get; set; }
}
public class Game
{
public int ID { get; set; }
public string Name { get; set;}
public List<Organizer> Organizers { get; set; }
}
I’m trying to grab all the Games that contain a specific Organizer. For instance, I might have some data like this:
Game
ID Name
-- ----
1 Soccer
2 Baseball
Organizer
ID Name
-- ----
1 John
2 Barry
GameOrganizer
GameID OrganizerID
------ -----------
1 1
2 1
And here’s the code I try to use:
Organizer thisOrganizer = db.Organizers.Single(o => o.ID == 1);
var gamesQuery = db.Games
.Where(game => game.Organizers.Contains(thisOrganizer))
.Select(g => new { ID = g.ID, Name = g.Name });
This gives me an error:
Only primitive types are supported in this context.
I understand what it means, but I can’t figure out how to express it in a different way. The closest I’ve come is:
var gamesQuery = db.Games
.Where(game => game.Organizers
.Any(gO => gO.ID == 1));
but that just returns nothing. I’ve looked everywhere but everything deals with string arrays that are already known in advance.
So, how can I express this without model objects?
You need to declare also Games as (navigation) property of Organizer, then it will be just: