【问题标题】:filter linq lookup based on values根据值过滤 linq 查找
【发布时间】:2011-02-09 21:09:50
【问题描述】:

我想根据值过滤 linq Lookup:

查找:

ILookup<int, Article> lookup

这是我目前所得到的,但不起作用:

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

只是为了澄清一下:我想获取所有具有包含在上述城市索引列表中的城市索引的文章。

【问题讨论】:

    标签: linq-to-objects ilookup


    【解决方案1】:

    您发布的代码的问题在于,您获取的所有文章都具有与具有匹配城市索引的任何文章相同的 ID。如果你只是先解包组,没有问题。

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

    或者

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

    【讨论】:

    • 非常感谢你澄清这个问题,很抱歉我不能投票给你...... :(
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多