2

I have the following enum:

public enum LifeCycle
{
    Pending = 0,
    Approved = 1,
    Rejected = 2,
}

And I want to create

Dictionary<int, string> LifeCycleDict;  

from the enum value and its toString

Is there a way to do it with linq?
(The equivelant to java's enum.values ) Thanks.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Bick
  • 17,833
  • 52
  • 146
  • 251
  • possible duplicate of [Enum to Dictionary c#](http://stackoverflow.com/questions/5583717/enum-to-dictionary-c-sharp) – Ani Mar 10 '13 at 16:11

1 Answers1

6
Dictionary<int, string> LifeCycleDict = Enum.GetNames(typeof(LifeCycle))
    .ToDictionary(Key => (int)Enum.Parse(typeof(LifeCycle), Key), value => value);

OR

Dictionary<int, string> LifeCycleDict = Enum.GetValues(typeof(LifeCycle)).Cast<int>()
    .ToDictionary(Key => Key, value => ((LifeCycle)value).ToString());

OR

Dictionary<int, string> LifeCycleDict = Enum.GetValues(typeof(LifeCycle)).Cast<LifeCycle>()
    .ToDictionary(t => (int)t, t => t.ToString());
Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62