简体   繁体   English

根据值过滤linq查找

[英]filter linq lookup based on values

I would like to filter a linq Lookup based on its values: 我想根据其值过滤linq Lookup:

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. 您发布的代码的问题在于,您获得的所有文章与具有匹配城市索引的任何文章具有相同的ID。 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM