How can I use Generics to create a way of making an IEnumerable from an enum?
- by Samantha J
Given an enum like this:
public enum City {
London = 1,
Liverpool = 20,
Leeds = 25
}
public enum House {
OneFloor = 1,
TwoFloors = 2
}
I am using the following code to give me an IEnumerable:
City[] values = (City[])Enum.GetValues(typeof(City));
var valuesWithNames = from value in values
select new { value = (int)value, name = value.ToString() };
The code works very good however I have to do this for quite a lot of enums. Is there a way I could create a generic way of doing this?