gpt4 book ai didi

linq - 从枚举填充字典

转载 作者:行者123 更新时间:2023-12-02 17:45:28 25 4
gpt4 key购买 nike

我有以下枚举:

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

我想创造

Dictionary<int, string> LifeCycleDict;  

来自 enum 值及其 toString

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

最佳答案

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());

关于linq - 从枚举填充字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15324384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com