从枚举填充字典

Bic*_*ick 2 linq enums dictionary

我有以下枚举:

public enum LifeCycle
{
    Pending = 0,
    Approved = 1,
    Rejected = 2,
}
Run Code Online (Sandbox Code Playgroud)

我想创造

Dictionary<int, string> LifeCycleDict;  
Run Code Online (Sandbox Code Playgroud)

enum价值和它toString

有没有办法用linq做到这一点?
(java的enum.values的equivelant)谢谢.

Pra*_*thi 6

Dictionary<int, string> LifeCycleDict = Enum.GetNames(typeof(LifeCycle))
    .ToDictionary(Key => (int)Enum.Parse(typeof(LifeCycle), Key), value => value);
Run Code Online (Sandbox Code Playgroud)

要么

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

要么

Dictionary<int, string> LifeCycleDict = Enum.GetValues(typeof(LifeCycle)).Cast<LifeCycle>()
    .ToDictionary(t => (int)t, t => t.ToString());
Run Code Online (Sandbox Code Playgroud)