2015-10-21 99 views
-1

我有數據作爲鍵值對的列表,例如說:列表鍵值對到字典

[0,"test"], 
[1,"test1"], 
[2,"test2"], 
[0,"test5"], 
[1,"test1"] 

我只是想這組數據加入到鍵值對作爲兩個獨立設置如下

keyvaluepair1 =>[0,"test"], 
[1,"test1"], 
[2,"test2"], 
keyvaluepair2 => [0,"test5"], 
[1,"test1"] 
+1

請明確說明分隔兩個集合的條件 –

+0

您到目前爲止嘗試過了什麼?請閱讀http://stackoverflow.com/help/how-to-ask – reto

回答

1

如果該模式是您的密鑰恢復爲0,則循環遍歷列表,並在每次重置時將新的字典添加到列表中。

//Your collection of KV Pair sets 
var listOfKvs = new List<Dictionary<int,string>() 

//an accumulator for the current set 
var currentKvs = new Dictionary<int,string>() 
var first = true; 

foreach (var kv in keyvalues) 
{ 
    //The condition for when your key resets 
    if (kv.Key == 0) 
    { 
     if (first) 
     { 
      //we don't store the first dicitonary because it should be empty 
      first = false; 
     } 
     else 
     { 
      listOfKvs.Add(currentKvs); 
     } 
     currentKvs = new Dictionary<int, string>() 
    } 

    currentKvs.add(kv); 
} 

//Store the last dictionary 
listOfKvs.add(currentKvs);