如何从枚举中创建IEnumerable [英] How can I create an IEnumerable from an enum
本文介绍了如何从枚举中创建IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定枚举如下:
public enum City
{
London = 1,
Liverpool = 20,
Leeds = 25
}
public enum House
{
OneFloor = 1,
TwoFloors = 2
}
如何将这些转换为具有名称为数据和值的两个字段的IEnumerable列表。有可能有一个通用的方法或这样做的方式吗?请不要这些值不总是顺序的。
How can I convert these into an IEnumerable lists with two fields named "data" and "value". Would it be possible to have a generic method or way of doing this? Please not that the values are not always sequential.
推荐答案
您可以使用 Enum.GetValues
:
You can use Enum.GetValues
:
City[] values = (City[])Enum.GetValues(typeof(City));
var valuesWithNames = from value in values
select new { value = (int)value, name = value.ToString() };
这篇关于如何从枚举中创建IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文