2015-06-19 217 views
-1

我有一個帶有倍數值的字典。我的字典是這樣的:嵌套字典的遞歸

public class checkitems: Dictionary<int, checkitems>{ 
    // .... 
} 

我正在循環這樣的每個級別。

foreach(KeyValuePair<int, checkitems> first in items){ 
    foreach(KeyValuePair<int, checkitems> second in first.values){ 
     foreach(KeyValuePair<int, checkitems> third in second.values){ 
      List<int> list1 = new List<int>(); 
      if(third.Value.Age > 20){ 
       list.Add(x.key) 
      } 
     } 
     foreach(var deleteKeys in list1){ 
     second.Value.Remove(deleteKeys) 
     } 
    } 
} 

目前我對每個級別的foreach寫入和檢查,看它是否滿足條件,然後將其添加到列表中刪除。我想知道如何遞歸地編寫它,以便我不必擔心關卡的深度。

Example data format: 
    Companies 
     i. Apple 
      a. key: Macbookpro Value: 200 
      b (key): IMAC  Value: 334 
      c (key): Iphone  Value : 12 
       1. (key) IOS8 Value : 13 
       2. (key) IOS7 Value : 15 
      d (key): Beats  Value: 20 
+0

我不能解密代碼。你能解釋一下你想完成什麼嗎?而不只是你想要使用遞歸。使用遞歸是(我希望)達到目的的一種手段。你的「結局」是什麼,你想讓你的代碼完成什麼? – sstan

+0

您提供的代碼沒有機會編譯 - 因此任何比您的代碼短的無效代碼都會這樣做......如果您需要真正的建議 - 請確保提供有效的代碼以及爲什麼它不能滿足您的需求。 –

+0

您能否提供'checkitems'的完整定義,並讓我們知道您的示例數據格式如何適用於'checkitems'? – Enigmativity

回答

1

我在理解你的代碼試圖實現什麼時做了我最好的。希望這有助於

using System; 
using System.Collections.Generic; 

namespace ConsoleApplication2 
{ 
    public static class Program 
    { 
     public static void RecurseCheckitems(CheckItems items) 
     { 
      List<Int32> l_deleteKeys = new List<Int32>(); 

      // Step 1: DFS - Down down down to the deepest level 
      foreach (Int32 key in items.Keys) 
      { 
       RecurseCheckitems(items[key]); 
      } 

      // Step 2: Extract all KEYS of Childelements having an AGE of at least 20 
      foreach (Int32 key in items.Keys) 
      { 
       l_deleteKeys.AddRange(DoCheckItems(items[key])); 
      } 

      // Step 3: Remove all extracted keys from the current Objecct 
      foreach (Int32 deleteKey in l_deleteKeys) 
      { 
       items.Remove(deleteKey); 
      } 
     } 

     /// <summary> 
     /// Helper-Function to extract the keys of Child-Elements having an age of at least 20 
     /// </summary> 
     /// <param name="item">Parent-Item to check</param> 
     /// <returns>List of KEYS of Child-Elements having an Age of at least 20</returns> 
     private static List<Int32> DoCheckItems(CheckItems item) 
     { 
      List<Int32> l = new List<Int32>(); 

      foreach (Int32 key in item.Keys) 
      { 
       if (item[key].Age > 20) 
       { 
        l.Add(key); 
       } 
      } 

      return l; 
     } 
    } 

    public sealed class CheckItems : Dictionary<Int32, CheckItems> 
    { 
     public Int32 Age { get; set; } 
    } 
} 
0

鑑於checkitems一個root例如,試試這個:

Func<checkitems, IEnumerable<Tuple<checkitems, checkitems>>> flatten = null; 
flatten = cis => 
    cis 
     .Select(x => Tuple.Create(cis, x.Value)) 
     .Concat(cis.SelectMany(c => flatten(c.Value))); 

foreach (var tuple in flatten(root) 
    .Where(x => x.Item2.Age > 20) 
    .ToArray()) 
{ 
    tuple.Item1.Remove(tuple.Item2); 
}