代码之家  ›  专栏  ›  技术社区  ›  brumScouse

使用LINQ省略字典值部分的一些条目,并将其投影到一个新的字典中,以维护原始键

  •  0
  • brumScouse  · 技术社区  · 16 年前

    我有一个通用的格言,其模板如下:

    Dictionary<object, IList<ISomeInterface>> dictionary1 = new Dictionary<object,    IList<ISomeInterface>>();  
    

    2 回复  |  直到 16 年前
        1
  •  1
  •   Jon Skeet    16 年前

    var result = original.ToDictionary(
        pair => pair.Key, // Key in result is key in original
        value => pair.Value  // Value in result is query on original value
                     .Where(item => !item.Contains("abc").ToList());
    

    someDictionary.ToDictionary(Function(x) x.key, _ 
                Function(y) y.Value.Where(not y.Value contains("abc").ToList())
    
        2
  •  0
  •   Grizzly    16 年前
    var result1 = dictionary1.Select(x => 
         {
              return new KeyValuePair<object, IList<ISomeInterface>>(
                         x.Key, 
                         x.Value.Where(y => y.Contains("abc")).ToList());
         }).ToDictionary(item => item.Key, item => item.Value);