我有一个数据列表,如键值对,例如:
[0,"test"],
[1,"test1"],
[2,"test2"],
[0,"test5"],
[1,"test1"]
我只想将这组数据添加到键值对中作为两个单独的设置如下
keyvaluepair1 =>[0,"test"],
[1,"test1"],
[2,"test2"],
keyvaluepair2 => [0,"test5"],
[1,"test1"]
答案 0 :(得分: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);