如何在 C# 中使用 LINQ 从字典转换为 SortedDictionary?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/451717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert from a Dictionary to a SortedDictionary using LINQ in C#?
提问by Guy
I have a Dictionary<string, double>
and I want to convert it to a SortedDictionary<double, string>
. How do I do this using LINQ extension methods in C# 3.0?
我有一个Dictionary<string, double>
,我想将它转换为一个SortedDictionary<double, string>
. 如何使用 C# 3.0 中的 LINQ 扩展方法执行此操作?
EDIT: Generic angle brackets not in original question when Marc and Jared answered.
编辑:当 Marc 和 Jared 回答时,原始问题中没有通用尖括号。
采纳答案by Marc Gravell
editthis answer was before edit; for an answer to the updated problem, see this reply.
编辑此答案是在编辑之前;有关更新问题的答案,请参阅此回复。
Why use LINQ? There is a constructor for this:
为什么要使用 LINQ?有一个构造函数:
new SortedDictionary<int, string>(existing);
You could adda ToSortedDictionary
- but I wouldn't bother...
你可以添加一个ToSortedDictionary
- 但我不会打扰......
回答by JaredPar
No LINQ is needed. SortedDictionary has a constructor to do the conversion.
不需要 LINQ。SortedDictionary 有一个构造函数来进行转换。
public SortedDictionary<TKey,TValue> Convert<TKey,TValue>(Dictionary<TKey,TValue> map) {
return new SortedDictionary<TKey,TValue>(map);
}
回答by Bryan Watts
You don't need LINQ, just some nifty extension methods:
你不需要 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);
}
Example usage:
用法示例:
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
回答by Andrew Hare
It seems as though you are asking for an elegant way to take a Dictionary<TKey,TValue>
and turn that into a SortedDictionary<TValue,TKey>
(note that the value of the Dictionary
is now the key of the SortedDictionary
). I don't see any answer addressing that so here it goes.
似乎您正在寻求一种优雅的方式将 aDictionary<TKey,TValue>
转换为 a SortedDictionary<TValue,TKey>
(请注意, the 的值Dictionary
现在是 the 的键SortedDictionary
)。我没有看到任何解决这个问题的答案,所以就这样了。
You could create an extension method that looks like this:
您可以创建一个如下所示的扩展方法:
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;
}
}
And your application code would look like this:
您的应用程序代码如下所示:
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());
}
}
回答by Bryan Watts
Inversion using ToDictionary
:
反转使用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);
}
Example usage:
用法示例:
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
Example of inverting and sorting (see my other answer for the definition of Sort
):
反转和排序的示例(有关 的定义,请参阅我的其他答案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