加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何从枚举中提取数据并使其成为IEnumerable?

发布时间:2020-12-16 00:21:18 所属栏目:百科 来源:网络整理
导读:我有以下枚举: public enum ReferenceKey { MenuType = 1,ReferenceStatus = 2,ContentType = 3} 有没有办法可以将此数据列为IEnumerable 两个领域.数字的第一个字段和第二个字段 第一个和第二个单词之间有空格的字符串,如下所示: 1 "Menu Type"2 "Referen
我有以下枚举:

public enum ReferenceKey {
    MenuType             = 1,ReferenceStatus      = 2,ContentType          = 3
}

有没有办法可以将此数据列为IEnumerable
两个领域.数字的第一个字段和第二个字段
第一个和第二个单词之间有空格的字符串,如下所示:

1  "Menu Type"
2  "Reference Status"
3  "Content Type"

解决方法

Is there a way that I can list this data as an IEnumerable with two fields. The first field for the number and the second for the string with a space between the first and second word

为什么不

解决方案2:您想要的阵列

IEnumerable<ReferenceKey> v = 
                       Enum.GetValues(typeof(ReferenceKey)).Cast<ReferenceKey>();

string[] result = 
                  v.Select(x => (int)x + " "" + x.ToString() + " "").ToArray();

看它工作

解决方案2:字典< int,string>

string[] str = Enum.GetNames(typeof(ReferenceKey));

Dictionary<int,string> lst = new Dictionary<int,string>(); 

for (int i = 0; i < str.Length; i++)
    lst.Add((int)(ReferenceKey)Enum.Parse(typeof(ReferenceKey),str[i]),str[i]);

看它工作

解决方案3:另一种创建Dictionary的方法< int,string>

Array v = Enum.GetValues(typeof(ReferenceKey));

Dictionary<int,string> lst = v.Cast<ReferenceKey>()
                               .ToDictionary(x => (int)x,x => x.ToString());

为此包含System.Linq命名空间

看它工作

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读