【发布时间】:2013-04-02 01:17:48
【问题描述】:
在使用 Code First 的 .NET4 MVC 4 中,我有两个类形成多对多关系。 如何编写一个简单的 LINQ 查询来从给定的流派列表中查找至少具有一种或多种流派的所有游戏? (即检索所有策略和/或沙盒类型的游戏)
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; }
}
请注意,我使用的是 Code First,因此我无法访问抽象连接表 GenreGames。
我已尝试以下方法从数组 gid 中找到包含至少一个流派 ID 的游戏:
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;
检索结果时
List<Game> similiarGames = result.ToList<Game>();
出现以下错误。
无法创建“GratifyGaming.Domain.Models.Genre”类型的常量值。此上下文仅支持原始类型或枚举类型。
如何仅使用原始类型或枚举类型获得我想要的结果?有解决办法吗?
【问题讨论】:
标签: c# asp.net-mvc linq asp.net-mvc-4 many-to-many