如何创建由枚举一个IEnumerable [英] How can I create an IEnumerable from an enum
本文介绍了如何创建由枚举一个IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
的在一个枚举结果
的如何使用泛型创建作出了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屋!
查看全文