In .NET4 MVC 4 using Code First I have two classes that form a many to many relationship.
How can I write a simple LINQ query to find all Games that have at least one or more genres from a given list of genres? (i.e. Retreive all games that are of the Strategy and/or Sandbox genre)
public class Game
{
public int ID { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
}
public class Genre
{
public int ID { get; set; }
public virtual ICollection<Game> Games { get; set; }
}
Please note that I am using Code First so I have no access to the abstracted join table GenreGames.
I have tried the following to find a game that contains at least one genre ID from the array gid:
var gid = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var result = from g in unitOfWork.GameRepository.dbSet
where (
from gen in unitOfWork.GenreRepository.dbSet
where gid.Contains(gen.ID)
select gen.ID
).Any()
select g;
When retrieving the results
List<Game> similiarGames = result.ToList<Game>();
the following error comes up.
Unable to create a constant value of type ‘GratifyGaming.Domain.Models.Genre’. Only primitive types or enumeration types are supported in this context.
How can I get the results I want using only primitive or enumeration types? Is there a workaround?
Try this instead :
Good Luck !!