How to retrieve Nth item in dictionary? [duplicate]
Asked Answered
B

4

0

Possible Duplicate:
How do I get the nth element from a Dictionary?

If there's a Dictionary with total of Y items and we need Nth item when N < Y then how to achieve this?

Example:

Dictionary<int, string> items = new Dictionary<int, string>();

items.add(2, "Bob");
items.add(5, "Joe");
items.add(9, "Eve");

// We have 3 items in the dictionary.
// How to retrieve the second one without knowing the Key?

string item = GetNthItem(items, 2);

How to write GetNthItem()?

Baldhead answered 17/6, 2011 at 10:37 Comment(6)
Dictionary<T, U>s don't have the concept of order built into them. If you need to know this information, Dictionary<T, U> (probably) isn't the right structure to use.Wholesale
It is not so useful getting the Nth item of a dictionary, since the order can change as you add new keys. Maybe the data structire you want to use is a SortedList<>Fai
@delv: I need to store the original index somewhere so a Dictionary is more appropriate than a List.Baldhead
@sbi: here I need the initial index. That question doesn't. So here OrderedDictionary is the answer when SortedList is appropriate there.Baldhead
@Felince: After initializing the Dictionary no item is added in my case.Baldhead
@Xaqron: Is there any reason to think your question is not answered by "The problem is a Dictionary isn't sorted. What you want is a SortedList, which allows you to get values by index as well as key"? This is, after all, pretty much exactly the same answer as the one you accepted here, only more elaborate.Resinoid
E
2

Dictionary isn't ordered. There is no nth item.

Use OrderedDictionary and Item()

Enenstein answered 17/6, 2011 at 10:40 Comment(0)
R
3

A Dictionary<K,V> doesn't have any intrinsic ordering, so there's really no such concept as the Nth item:

For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<TKey, TValue> structure representing a value and its key. The order in which the items are returned is undefined.

Having said that, if you just want the item that arbitrarily happens to be found at position N right now then you could use ElementAt:

string item = items.ElementAt(2).Value;

(Note that there's no guarantee that the same item will be found in the same position if you run the same code again, or even if you call ElementAt twice in quick succession.)

Rule answered 17/6, 2011 at 10:40 Comment(0)
E
2

Dictionary isn't ordered. There is no nth item.

Use OrderedDictionary and Item()

Enenstein answered 17/6, 2011 at 10:40 Comment(0)
C
1

Using LINQ:

Dictionary<int, string> items = new Dictionary<int, string>();

items.add(2, "Bob");
items.add(5, "Joe");
items.add(9, "Eve");

string item = items.Items.Skip(1).First();

You might want to use FirstOrDefault instead of First, depending on how much you know about your data.

Also, be aware that while dictionary does need an ordering for its items (otherwise it wouldn't be able to iterate over them), that ordering is a simple FIFO (it couldn't easily be anything else, since IDictionary does not require your items to be IComparable).

Crossbred answered 17/6, 2011 at 10:39 Comment(0)
C
0

string item = items[items.Keys[1]];

However, be aware that a dictionary isn't sorted. Depending on your requirements, you could use a SortedDictionary.

Coed answered 17/6, 2011 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.