Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(1, 158);
dic.Add(5, 25);
dic.Add(3, 215);
dic.Add(2, 369);
dic.Add(4, 147);
var result = from pair in dic orderby pair.Key select pair;
foreach (KeyValuePair<int, int> pair in result)
{
Console.WriteLine("Key:{0}, Value:{1}", pair.Key, pair.Value);
}
Console.ReadKey();
现在结果是
1
2
3
4
5
我希望是
5
4
3
2
1
5 个解决方案
#1
var result = from pair in dic orderby pair.Key
descending select pair;
#2
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(1, 158);
dic.Add(5, 25);
dic.Add(3, 215);
dic.Add(2, 369);
dic.Add(4, 147);
var result = from pair in dic orderby pair.Key descending select pair;
foreach (KeyValuePair<int, int> pair in result)
{
Console.WriteLine("Key:{0}, Value:{1}", pair.Key, pair.Value);
}
Console.ReadKey();
#3
呵呵,LINQ基本和SQL一样的。
#4
var dicTest2 = new Dictionary<string, string>{{"test3","test3"},{"test4", "test4"},{"test5","test5"},{"test6","test6"}};
var dicTest3= dicTest2.Reverse();
foreach (var keyValuePair in dicTest3)
{
Console.WriteLine("{0}:{1}", keyValuePair.Key, keyValuePair.Value);
}
var dicTest3= dicTest2.Reverse();
foreach (var keyValuePair in dicTest3)
{
Console.WriteLine("{0}:{1}", keyValuePair.Key, keyValuePair.Value);
}
#5
Dictionary<string,string> dict=dicTest3.ToDictionary(p => p.Key,p=>p.Value);
#1
var result = from pair in dic orderby pair.Key
descending select pair;
#2
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(1, 158);
dic.Add(5, 25);
dic.Add(3, 215);
dic.Add(2, 369);
dic.Add(4, 147);
var result = from pair in dic orderby pair.Key descending select pair;
foreach (KeyValuePair<int, int> pair in result)
{
Console.WriteLine("Key:{0}, Value:{1}", pair.Key, pair.Value);
}
Console.ReadKey();
#3
呵呵,LINQ基本和SQL一样的。
#4
var dicTest2 = new Dictionary<string, string>{{"test3","test3"},{"test4", "test4"},{"test5","test5"},{"test6","test6"}};
var dicTest3= dicTest2.Reverse();
foreach (var keyValuePair in dicTest3)
{
Console.WriteLine("{0}:{1}", keyValuePair.Key, keyValuePair.Value);
}
var dicTest3= dicTest2.Reverse();
foreach (var keyValuePair in dicTest3)
{
Console.WriteLine("{0}:{1}", keyValuePair.Key, keyValuePair.Value);
}
#5
Dictionary<string,string> dict=dicTest3.ToDictionary(p => p.Key,p=>p.Value);