2

我有以下枚举:

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

我想创造

Dictionary<int, string> LifeCycleDict;  

enum价值及其toString

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

4

1 回答 1

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

或者

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

或者

Dictionary<int, string> LifeCycleDict = Enum.GetValues(typeof(LifeCycle)).Cast<LifeCycle>()
    .ToDictionary(t => (int)t, t => t.ToString());
于 2013-03-10T16:38:16.983 回答