首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

sortlist的超难度有关问题

2012-09-16 
sortlist的超难度问题我有一个sortedlist对象,键值分别是1 one 2 two 3 three 现在想在1和2之间插入键值为

sortlist的超难度问题
我有一个sortedlist对象,键值分别是1 one 2 two 3 three 现在想在1和2之间插入键值为2 two.one,使之变为1 one 2 two.one 3 two 4 three,请问如何实现。还有如果更改其中的键值?

[解决办法]

C# code
 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>             */ 

热点排行