2015-10-21 313 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);