大家好,请教一个问题,我定义了一个Dictionary,代码如下 Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("a", "w");
test.Add("z", "w");
test.Add("s", "w");
我希望根据主键对 test中的元素进行排序,结果如下test[0]: ("a","w")test[1]: ("s","w")test[2]: ("z","w")
请问如何排序?请问是不是如果Dictionary中的元素是已经排序好的,用test["s"]查找更快?多谢大家指教
test.Add("a", "w");
test.Add("z", "w");
test.Add("s", "w");
我希望根据主键对 test中的元素进行排序,结果如下test[0]: ("a","w")test[1]: ("s","w")test[2]: ("z","w")
请问如何排序?请问是不是如果Dictionary中的元素是已经排序好的,用test["s"]查找更快?多谢大家指教
解决方案 »
- 问一个关于sql的问题
- FusionCharts 图无法显示出来
- 一个有关Access数据表中进行土地分类面积汇总的问题
- C# 如何将dataset导出到一个excel的不同sheet中
- [C#]请问如何获得变量名的字符串,谢谢~
- 如何在.Net中调用Java API,COM组件,COM+组件?
- powershell 管理exchange2007服务器
- 我的SQL执行外键约束语句的时候怎么总是会说引用无效表?帮帮忙
- 请教如何实现点击系统状态栏的窗体小图标让窗体最小化?
- 如何取得treeView所选节点的值,
- 如何将文件夹显示在ListView上,双击并能在ListView进入文件夹
- 100求 winform窗体自适应显示器分辨率,能等比缩放
using System.Collections.Generic;
using System.Linq;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("a", "w");
test.Add("z", "w");
test.Add("s", "w"); foreach (KeyValuePair<string, string> t in test)
{
Console.WriteLine("Key:"+t.Key + " Value:" +t.Value);
} var temp = test.OrderBy(x => x.Key); Console.WriteLine(); foreach (KeyValuePair<string, string> t in temp)
{
Console.WriteLine("Key:" + t.Key + " Value:" + t.Value);
} Console.Read();
}
}
}
static void Main(string[] args)
{
Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("a", "w");
test.Add("z", "w");
test.Add("s", "w"); var q = from c in test
orderby c.Key ascending
select c; foreach (KeyValuePair<string, string> item in q)
{
Console.WriteLine("key={0} value={1}", item.Key, item.Value);
}
Console.Read();
}