我想要获得满足所有genreIds包含在List<int>
中的gameIds列表。使用Linq获取匹配ID列表的结果
的表(部分):
editorial_list:
- game_id
- 内容
game_genres(游戏可以属于多个流派):
- ID
- game_id
- genre_id
我需要存在在game_genres表中的所有genre_id
的游戏的游戏ID的列表。
例如:
的genre_id
列表的包括类型2和3
如果游戏ID = 14存在于game_genres
表两者genre_id
= 2和genre_id
= 3。因此,它将被包含在最终结果中。
这里是我的代码:
// get the list of game_id's that have an editorial
var editorialList = (from ee in db.editorials where ee.is_enabled == true select new {
game_id = ee.game_id
}).ToList();
// Produce a list of the games in the editorials and their genre Ids that the belong to
var gameAndGenres = (from el in editorialList join gg in db.game_genres
on el.game_id equals gg.game_id
select new {
game_id = el.game_id,
genre_id = gg.genre_id
}
);
var res = gameAndGenres.Where(
x => x.genres.Contains(x.genre_id)) == genres.Count; // stuck here
最终结果应该game_id的独特的列表,在列表每场比赛属于所有这是在体裁List<int>
列出的流派。
我创建了几个步骤来帮助我理解查询,但它可能能够在一行中解决,我只是无法解决它。
更新:这是我尝试的新代码。
var res = gameAndGenres.GroupBy(x => x.game_id)
.Select(g => new {
game_id = g.Key,
genreIds = g.Select(c => c.genre_id)
});
var res2 = res.Where(x => genres.Intersect(x.genreIds).Count()
== genres.Count()).ToList();
因为game_genres有流派的所有GAEMS这将是非常不见效,我只是希望那些谁有编辑内容。 –