Updated on Kisan Patel
This tutorial will show you how to convert an enum to a generic list in C#?
For example, we have enum as shown in below code:
public enum Fruit { Apple = 1, Banana = 2, Grapes = 3 }
Now, we have convert above enum to IEnumerable<Fruit> using below line of code:
Enum.GetValues(typeof(Fruit)).Cast<Fruit>();
So, If you want to convert enum into List<SelectListItem> then use below line of code:
List<SelectListItem> = Enum.GetValues(typeof(Fruit)).Cast<Fruit>() .Select(x => new SelectListItem { Value = Convert.ToString((int)x), Text = x.ToString() }).ToList();