我想要獲得滿足所有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這將是非常不見效,我只是希望那些誰有編輯內容。 –