0
public static OrderedDictionary Drinks = new OrderedDictionary();
Drinks["Water"] = 3;
Drinks["Coffee"] = 2
Drinks["Beer"] = 5;

How can I get Item name by position? I want when i specify position 1 to get "Water".

2 Answers2

1

Try this :

For value:

 var result= (Drinks.Cast<DictionaryEntry>().ElementAt(2)).Value;

For Key

var result= (Drinks.Cast<DictionaryEntry>().ElementAt(2)).Key;
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • Close. OP would like the `Key`, rather than the `Value`. Other than that, this works as requested - http://ideone.com/rmAU1h – CodingIntrigue Feb 06 '14 at 12:42
1

Try this,

var result = Drinks.Keys.OfType<string>().ElementAt(0);

This will return first key of OrderedDictionary. Here you are sure that your key type is string. but if you are not sure data type of key then use object

like,

var result = Drinks.Keys.OfType<object>().ElementAt(0);
Jignesh Thakker
  • 3,638
  • 2
  • 28
  • 35