Here is the extension method I use to convert enumerations. The only difference is that I am returning IEnumerbale<KeyValuePair<int, int>> for my purpose:
public static IEnumerable<KeyValuePair<int, string>> ToListOfKeyValuePairs<TEnum>(this TEnum enumeration) where TEnum : struct
{
return from TEnum e in Enum.GetValues(typeof(TEnum))
select new KeyValuePair<int, string>
(
(int)Enum.Parse(typeof(TEnum), e.ToString()),
Regex.Replace(e.ToString(), "[A-Z]", x => string.Concat(" ", x.Value[0])).Trim()
);
}
It also adds spaces for the value.
Example:
enum Province
{
BritishColumbia = 0,
Ontario = 1
}
Usage:
<select>
<% foreach(var item in Province.BritishColumbia.ToListOfKeyValuePairs()){ %>
<option value="<%=item.Key %>"><%=item.Value %></option>
<% } %>
</select>
Output:
<select>
<option value="0">British Columbia</option>
<option value="1">Ontario</option>
</select>
Though @Paul Ruane is correct I have found this to be a very useful extension method. It's not a perfect world.