私はList<int>
に含まれるすべてのgenreIdsを満たすgameIdのリストを取得しようとしています。Linqを使ってIDのリストと一致する結果を取得
テーブル(部分):
editorial_list:
- game_id
- コンテンツ
game_genres(ゲームはいくつかのジャンルに属することができます):
- ID
- game_id
- は、私がgame_genresテーブル内のすべての
genre_id
年代のために存在するゲームのゲームIDのリストを取得する必要があり
をgenre_id。例えば
:ゲームIdは= 14 = 2 genre_id
と= 3 genre_id
の両方のためにgame_genres
テーブルに存在する場合
genre_id
年代のリストは、それがそうジャンル2及び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
エンドはがリスト内の各ゲームはジャンルList<int>
にリストされたすべてののジャンルに属していることを、game_idの独特のリストでなければなりません結果。
クエリを理解するのに役立ついくつかの手順を作成しましたが、1行で解決できる可能性があり、解決できませんでした。
更新:これは私が試みている新しいコードです。 Game
がゼロ以上のジャンルに属することができますし、Genre
がゼロ以上のゲームを持つことができます。
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();
これは非常に効果的ではないだろう、私はちょうど人々が欲しいです編集内容があります。 –