Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Dictionary<string,string> how to loop through items without knowing key [duplicate]

Tags:

c#

idictionary

I have a:

var selectedDates = new Dictionary<string, string>();
selectedDates.Add("2014-06-21", DateTime.Now.ToLongDateString());
selectedDates.Add("2014-07-21", DateTime.Now.AddDays(5).ToLongDateString());
selectedDates.Add("2014-08-21", DateTime.Now.AddDays(9).ToLongDateString());
selectedDates.Add("2014-09-21", DateTime.Now.AddDays(14).ToLongDateString());

How can I loop trough items without knowing the key?

For example I want to get the value of the item[0]

If I do:

var item = selectedDates[0].value; // I get an error
like image 901
VAAA Avatar asked Jul 28 '14 14:07

VAAA


5 Answers

How can I loop trough items without knowing the key?

For example I want to get the value of the item[0]

You want to treat the dictionary as (ordered) collection similar to a list or array and get the first item in it?

You can because a Dictionary<string, string> is an IEnumerable<KeyValuePair<string, string>> implicitly. Just use First or FirstOrDefault:

string valueAtFirstPosition = selectedDates.First().Value; 

However, note that a dictionary is not meant to be used as as an ordered collection. It is a collection which can be used to fast-lookup a value by a key. But you can enumerate it anyway.

foreach(KeyValuePair<string, string>keyVal in selectedDates)
{
    Console.WriteLine("Key: {0} Value: {1}", keyVal.Key, keyVal.Value);
}

You should simply not rely on that order. I think in the current implementation the order is stable as long as you don't delete items. Read

Read: Why is a Dictionary “not ordered”?

like image 58
Tim Schmelter Avatar answered Nov 15 '22 07:11

Tim Schmelter


try this

  foreach (string key in selectedDates.Keys)
  {
        var item = selectedDates[key]; 
  }
like image 26
nsgocev Avatar answered Nov 15 '22 09:11

nsgocev


try this :

foreach(var key in selectedDates.Keys)
{
    var value = selectedDates[key];
}
like image 30
Parimal Raj Avatar answered Nov 15 '22 09:11

Parimal Raj


It's simple, loop trough it with a foreach or to get a specific index do:

var date = selectedDates.ElementAt(0).Value;
like image 40
Vincent Avatar answered Nov 15 '22 08:11

Vincent


Let me put together two things for you. Firstly, you can loop or use LINQ to access elements, just as you could do it in a list as well:

var dict = new Dictionary<string, string>();

// loop
foreach (var item in dict)
{
    var key = item.Key;
    var value = item.Value;
}

// "first" (see below)
var firstItem = dict.First();

However, be aware that what you're referring to as the first item can be pretty much any item in the Dictionary. Dictionaries store elements in any order that is convenient for a lookup (so do sets).

This order is known for some implementations, but lists or arrays might fit better when the order of the elements is important. A Dictionary in .NET is an implementation of a hash table data structure (tree map is another map implementation).

like image 30
Matthias Meid Avatar answered Nov 15 '22 07:11

Matthias Meid