C# 从字典中获取第一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13979966/
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
Get first element from a dictionary
提问by cdub
I have the following declaration:
我有以下声明:
Dictionary<string, Dictionary<string, string>> like = new Dictionary<string, Dictionary<string, string>>();
I need to get the first element out, but do not know the key or value. What's the best way to do this?
我需要取出第一个元素,但不知道键或值。做到这一点的最佳方法是什么?
采纳答案by McGarnagle
Note that to call First
here is actually to call a Linq extension of IEnumerable, which is implemented by Dictionary<TKey,TValue>
. But for a Dictionary, "first" doesn't have a defined meaning. According to this answer, the last item added ends up being the "First" (in other words, it behaves like a Stack), but that is implementation specific, it's not the guaranteedbehavior. In other words, to assumeyou're going to get any defined item by calling Firstwould be to beg for trouble -- using it should be treated as akin to getting a randomitem from the Dictionary, as noted by Bobson below. However, sometimes this is useful, as you just need anyitem from the Dictionary.
注意First
这里调用实际上是调用了IEnumerable的一个Linq扩展,由Dictionary<TKey,TValue>
. 但是对于字典,“第一”没有明确的含义。根据这个答案,添加的最后一项最终是“第一个”(换句话说,它的行为类似于Stack),但这是特定于实现的,它不是保证的行为。换句话说,假设您将通过调用First来获取任何定义的项目是自找麻烦——使用它应该被视为类似于从字典中获取随机项目,如下面的 Bobson 所指出的。但是,有时这很有用,因为您只需要任何字典中的项目。
Just use the Linq First()
:
只需使用 Linq First()
:
var first = like.First();
string key = first.Key;
Dictionary<string,string> val = first.Value;
Note that using First
on a dictionary gives you a KeyValuePair
, in this case KeyValuePair<string, Dictionary<string,string>>
.
请注意,在本例中,在First
字典上使用会为您提供.KeyValuePair
KeyValuePair<string, Dictionary<string,string>>
Note also that you couldderive a specific meaning from the use of First
by combining it with the Linq OrderBy
:
另请注意,您可以First
通过将 与 Linq 结合使用来获得特定含义OrderBy
:
var first = like.OrderBy(kvp => kvp.Key).First();
回答by Agustin Meriles
Dictionary<string, Dictionary<string, string>> like = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> first = like.Values.First();
回答by Alexei Levenkov
Dictionary does not define order of items. If you just need an item use Keys
or Values
properties of dictionary to pick one.
字典没有定义项目的顺序。如果您只需要一个项目使用Keys
或Values
字典的属性来选择一个。
回答by RAS
Though you can use First()
, Dictionaries do not have order per se. Please use OrderedDictionaryinstead. And then you can do FirstOrDefault
. This way it will be meaningful.
虽然您可以使用First()
,但字典本身没有顺序。请改用OrderedDictionary。然后你就可以了FirstOrDefault
。这样才会有意义。
回答by naren.katneni
EDIT: Use an OrderedDictionary.
编辑:使用 OrderedDictionary。
It's better to use FirstOrDefault()
to retrieve the first value.
最好用于FirstOrDefault()
检索第一个值。
Ex:
前任:
var firstElement = like.FirstOrDefault();
string firstElementKey = firstElement.Key;
Dictinary<string,string> firstElementValue = firstElement.Value;
回答by JesseBuesking
For anyone coming to this that wants a linq-less way to get anelement from a dictionary
对于任何人来此想要一个LINQ少的方式来获得一个从字典元素
var d = new Dictionary<string, string>();
d.Add("a", "b");
var e = d.GetEnumerator();
e.MoveNext();
var anElement = e.Current;
// anElement/e.Current is a KeyValuePair<string,string>
// where Key = "a", Value = "b"
I'm not sure if this is implementation specific, but if your Dictionary doesn't have any elements, Current
will contain a KeyValuePair<string, string>
where both the key and value are null
.
我不确定这是否是特定于实现的,但是如果您的 Dictionary 没有任何元素,Current
则将包含KeyValuePair<string, string>
键和值都为null
.
(I looked at the logic behind linq's First
method to come up with this, and tested it via LinqPad 4
)
(我查看了 linqFirst
方法背后的逻辑来提出这个,并通过 进行了测试LinqPad 4
)
回答by Levris First
ill find easy way to find first element in Dictionary :)
找不到在字典中找到第一个元素的简单方法:)
Dictionary<string, Dictionary<string, string>> like =
newDictionary<string,Dictionary<string, string>>();
foreach(KeyValuePair<string, Dictionary<string, string>> _element in like)
{
Console.WriteLine(_element.Key); // or do something
break;
}
回答by Martinxw
convert to Array
转换为数组
var array = like.ToArray();
var first = array[0];