Reputation: 25711
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?
Upvotes: 77
Views: 246942
Reputation: 389
Easy way of to index a Collection in terms of performance, high compatibility (2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8) and easy implemention.
Save today!! Its not only a items copy, this is items reference of a Collection!! buy it!!
string [] arrayString = new string[like.Count];
like.Values.CopyTo( arrayString,0 );
arrayString[0] //First
References:
Upvotes: 0
Reputation: 4854
using System.Linq;
Dictionary<string, Dictionary<string, string>> like = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> first = like.Values.First();
Upvotes: 3
Reputation: 13
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;
}
Upvotes: 1
Reputation: 102753
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 guaranteed behavior. In other words, to assume you're going to get any defined item by calling First would be to beg for trouble -- using it should be treated as akin to getting a random item from the Dictionary, as noted by Bobson below. However, sometimes this is useful, as you just need any item from the Dictionary.
Just use the 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>>
.
Note also that you could derive a specific meaning from the use of First
by combining it with the Linq OrderBy
:
var first = like.OrderBy(kvp => kvp.Key).First();
Upvotes: 138
Reputation: 6586
For anyone coming to this that wants a linq-less way to get an element from a dictionary
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
.
(I looked at the logic behind linq's First
method to come up with this, and tested it via LinqPad 4
)
Upvotes: 33
Reputation: 3385
Though you can use First()
, Dictionaries do not have order per se. Please use OrderedDictionary instead. And then you can do FirstOrDefault
. This way it will be meaningful.
Upvotes: 16
Reputation: 285
EDIT: Use an OrderedDictionary.
It's better to use FirstOrDefault()
to retrieve the first value.
Ex:
var firstElement = like.FirstOrDefault();
string firstElementKey = firstElement.Key;
Dictinary<string,string> firstElementValue = firstElement.Value;
Upvotes: 5
Reputation: 100547
Dictionary does not define order of items. If you just need an item use Keys
or Values
properties of dictionary to pick one.
Upvotes: 2