Finding values ​​based linq filter

I would like to filter linq Lookup based on its values:

Search:

ILookup<int, Article> lookup

here's what i still have that doesn't work:

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);

to clarify: I want to get all the articles with the city index, which is contained in the city index above.

+3
source share
1 answer

The problem with the code you posted is that you get all the articles with the same identifier as any article with the corresponding city index. If you unpack groups first, no problem.

IList<int> cityIndexes = GetCityIndexesByNames(cities);

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

or

lookup =
(
  from g in lookup
  from article in g
  where cityIndexes.Contains((int)article.ArticleCity)))
  select article
).ToLookup(article => (int)article.ArticleParentIndex); 
+5
source

Source: https://habr.com/ru/post/1791069/