4

linqルックアップをその値に基づいてフィルタリングしたいと思います。

ルックアップ:

ILookup<int, Article> lookup

これが私がこれまでに持っているもので、機能していないものです:

IList<int> cityIndexes = GetCityIndexesByNames(cities);    

lookup = lookup
                .Where(p => p.Any(x => cityIndexes.Contains((int)x.ArticleCity)))
                .SelectMany(l => l)
                .ToLookup(l => (int)l.ArticleParentIndex, l => l);

明確にするために:上記の都市インデックスリストに含まれている都市インデックスを持つすべての記事を取得したいと思います。

4

1 に答える 1

7

投稿したコードの問題は、都市インデックスが一致する記事と同じIDを持つすべての記事を取得していることです。最初にグループを解凍するだけであれば、問題はありません。

IList<int> cityIndexes = GetCityIndexesByNames(cities);

lookup = lookup
  .SelectMany(g => g)
  .Where(article => cityIndexes.Contains((int)article.ArticleCity)))
  .ToLookup(article => (int)article.ArticleParentIndex); 

または

lookup =
(
  from g in lookup
  from article in g
  where cityIndexes.Contains((int)article.ArticleCity)))
  select article
).ToLookup(article => (int)article.ArticleParentIndex); 
于 2011-02-09T21:18:44.197 に答える