且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将OrderedDictionary在C#中使用LINQ(使用.net 3.5)进行排序?

更新时间:2023-11-23 16:04:34

下面就给您根据您OrderedDictionary排序的字典。

Following will give you a sorted dictionary based on your OrderedDictionary.

var sortedDictionary= od.Cast<DictionaryEntry>()
                       .OrderBy(r=> r.Value)
                       .ToDictionary(c=> c.Key, d=> d.Value);

输出:

foreach (var entry in sortedDictionary)
    Console.WriteLine("Key: {0} Value: {1}", entry.Key, entry.Value);

Key: a3 Value: 2
Key: a1 Value: 3
Key: a4 Value: 4
Key: a2 Value: 5