I would like to filter a linq Lookup based on its values:
the lookup:
ILookup<int, Article> lookup
here's what I've got so far which isn't working:
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);
just to clarify: I want to get all the articles with a city index that is contained in the above city index list.
The problem with the code you posted, is that you're getting all the articles with the same ID as any article that has a matching city index. If you just unpack the groups first, there's 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);