C# List Insert用法

风一样的大叔 2012-05-08 06:45:39
string[] tableArray = query.tablestr.Split(',');
Dictionary<string, int> Lengths = new Dictionary<string, int>();
for (int n = 0; n < tableArray.Length - 1; n++)
{
for (int u = 0; u < TableLengths.Count; u++)
{
if (TableLengths[u].Name == tableArray[n])
{
Lengths.Add(TableLengths[u].Name, TableLengths[u].Length);
}
}
}
var list = (from d in Lengths orderby d.Value ascending select d).ToList();//排序
//我现在要对list进行插入操作
//请问怎么在list中插入<string, int>这种类型的元素啊
...全文
952 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
风一样的大叔 2012-05-09
  • 打赏
  • 举报
回复
可以了,谢谢
for (var i = 0; i < list.Count; i++)
{
switch (list[i].Key)
{
case "PRO_Verticalstretch_end":
list.Insert(i, new KeyValuePair<string, int>("ss",5));
break;
default:
break;
}
}
baiwenyu 2012-05-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
C# code
string[] tableArray = query.tablestr.Split(',');
Dictionary<string, int> Lengths = new Dictionary<string, int>();
for (int n = 0; n < tableArray.Length - 1; n++)
……
[/Quote]
正解 Dictionary 的每一项的类型 就是 KeyValuePair
wangsong145 2012-05-08
  • 打赏
  • 举报
回复
list好像只能插入一个值吧,楼主要实现插入<stirng,int>这种类型的数据得通过IDictionary或者是HashTable
bdmh 2012-05-08
  • 打赏
  • 举报
回复

Dictionary<string, int> Lengths = new Dictionary<string, int>();
Lengths.Add("a", 1);
Lengths.Add("b", 2);
Lengths.Add("c", 3);
var list = (from d in Lengths orderby d.Value ascending select d).ToList();
list.Add(new KeyValuePair<string, int>("d", 4));

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧