我有一个Dictionary<string, double>
,我想将它转换为SortedDictionary<double, string>
。我如何在C#3.0中使用LINQ扩展方法做到这一点?如何使用C#中的LINQ将字典转换为SortedDictionary?
编辑:当Marc和Jared回答时,通用尖括号不在原始问题中。
我有一个Dictionary<string, double>
,我想将它转换为SortedDictionary<double, string>
。我如何在C#3.0中使用LINQ扩展方法做到这一点?如何使用C#中的LINQ将字典转换为SortedDictionary?
编辑:当Marc和Jared回答时,通用尖括号不在原始问题中。
编辑此答案已被编辑;有关更新问题的答案,请参阅this reply。
为什么要使用LINQ?没有此构造函数:
new SortedDictionary<int, string>(existing);
你可以添加一个ToSortedDictionary
- 但我不会理会......
打我20秒 – JaredPar 2009-01-16 19:58:02
无需LINQ。 SortedDictionary有一个构造函数来完成转换。
public SortedDictionary<TKey,TValue> Convert<TKey,TValue>(Dictionary<TKey,TValue> map) {
return new SortedDictionary<TKey,TValue>(map);
}
你不需要LINQ,只是一些漂亮的扩展方法:
public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if(dictionary == null)
{
throw new ArgumentNullException("dictionary");
}
return new SortedDictionary<TKey, TValue>(dictionary);
}
public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
{
if(dictionary == null)
{
throw new ArgumentNullException("dictionary");
}
if(comparer == null)
{
throw new ArgumentNullException("comparer");
}
return new SortedDictionary<TKey, TValue>(dictionary, comparer);
}
用法示例:
var dictionary = new Dictionary<int, string>
{
{ 1, "one" },
{ 2, "two" },
{ 0, "zero" }
};
foreach(var pair in dictionary.Sort())
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
// 0: zero
// 1: one
// 2: two
它好像你所要求的一种优雅的方式来取一个Dictionary<TKey,TValue>
并将其转化为SortedDictionary<TValue,TKey>
(请注意Dictionary
的值现在是SortedDictionary
的关键)。我没有看到任何解决这个问题的答案。
您可以创建一个看起来像这样的扩展方法:
static class Extensions
{
public static Dictionary<TValue, TKey>
AsInverted<TKey, TValue>(this Dictionary<TKey, TValue> source)
{
var inverted = new Dictionary<TValue, TKey>();
foreach (KeyValuePair<TKey, TValue> key in source)
inverted.Add(key.Value, key.Key);
return inverted;
}
}
和你的应用应该是这样的:
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
var dict = new Dictionary<String, Double>();
dict.Add("four", 4);
dict.Add("three", 3);
dict.Add("two", 2);
dict.Add("five", 5);
dict.Add("one", 1);
var sortedDict = new SortedDictionary<Double, String>(dict.AsInverted());
}
}
使用ToDictionary
反转:
public static IDictionary<TValue, TKey> Invert<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if(dictionary == null)
{
throw new ArgumentNullException("dictionary");
}
return dictionary.ToDictionary(pair => pair.Value, pair => pair.Key);
}
例用法:
var dictionary = new Dictionary<string, int>
{
{ "zero", 0 },
{ "one", 1 },
{ "two", 2 }
};
foreach(var pair in dictionary.Invert())
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
// 0: zero
// 1: one
// 2: two
反相的实例和排序(看我为的Sort
定义其他的答案):
var dictionary = new Dictionary<string, int>
{
{ "one", 1 },
{ "two", 2 },
{ "zero", 0 }
};
foreach(var pair in dictionary.Invert().Sort())
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
// 0: zero
// 1: one
// 2: two
您的意思是SortedDictionary <字符串,双重>? – 2009-01-16 20:04:44