sortlist的超难度问题
我有一个sortedlist对象,键值分别是1 one 2 two 3 three 现在想在1和2之间插入键值为2 two.one,使之变为1 one 2 two.one 3 two 4 three,请问如何实现。还有如果更改其中的键值?
[解决办法]
int pos=1;//定义位置 Dictionary<int, string> sortedlist = new Dictionary<int, string>() { {1,"one"}, {2,"two"}, {3,"three"} }; Dictionary<int, string> sortedlist1 = sortedlist.Take(pos).ToDictionary(c=>c.Key,c=>c.Value); sortedlist1.Add(2,"two.one"); Dictionary<int, string> sortedlist2 = sortedlist.Skip(pos).ToDictionary(c => c.Key+1, c => c.Value); Dictionary<int, string> resultlist = sortedlist1.Concat(sortedlist2).ToDictionary(c => c.Key, c => c.Value); /* * + [0] {[1, one]} System.Collections.Generic.KeyValuePair<int,string> + [1] {[2, two.one]} System.Collections.Generic.KeyValuePair<int,string> + [2] {[3, two]} System.Collections.Generic.KeyValuePair<int,string> + [3] {[4, three]} System.Collections.Generic.KeyValuePair<int,string> */