Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a specific key from within a hashtable

Tags:

So I have this Hashtable

Hashtable Months = new Hashtable();
Months.Add(0, "JANUARY");
Months.Add(1, "FEBRUARY");
Months.Add(2, "MARCH");
Months.Add(3, "APRIL");
Months.Add(4, "MAY");
Months.Add(5, "JUNE");
Months.Add(6, "JULY");
Months.Add(7, "AUGUST");
Months.Add(8, "SEPTEMBER");
Months.Add(9, "OCTOBER");
Months.Add(10, "NOVEMBER");
Months.Add(11, "DECEMBER");

I would like for the user to enter a month e.g."May" the be able to retrieve index[4] from an array within my program.

string Month = Console.ReadLine();

Basically to retrieve the index from the number of the corresponding Month entered.

like image 849
K.John Avatar asked Dec 15 '17 15:12

K.John


1 Answers

Try this

var key = Months.Keys.Cast<int>().FirstOrDefault(v => Months[v] == "MAY");

Note: Don't forget to include this namespace - using System.Linq;

like image 59
Krishnraj Rana Avatar answered Oct 11 '22 22:10

Krishnraj Rana