开发者

filter linq lookup based on values

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 = GetCityInde开发者_StackOverflowxesByNames(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); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜