根据值过滤linq查找

use*_*498 4 linq-to-objects ilookup

我想根据其值过滤linq Lookup:

查找:

ILookup<int, Article> lookup
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所做的不起作用:

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);
Run Code Online (Sandbox Code Playgroud)

只是为了澄清:我想获得所有带有上述城市索引列表中包含的城市索引的文章.

Amy*_*y B 6

您发布的代码的问题在于,您获得的所有文章与具有匹配城市索引的任何文章具有相同的ID.如果你先打开组,那就没问题了.

IList<int> cityIndexes = GetCityIndexesByNames(cities);

lookup = lookup
  .SelectMany(g => g)
  .Where(article => cityIndexes.Contains((int)article.ArticleCity)))
  .ToLookup(article => (int)article.ArticleParentIndex); 
Run Code Online (Sandbox Code Playgroud)

要么

lookup =
(
  from g in lookup
  from article in g
  where cityIndexes.Contains((int)article.ArticleCity)))
  select article
).ToLookup(article => (int)article.ArticleParentIndex); 
Run Code Online (Sandbox Code Playgroud)