Dec 20, 2016

Convert list of string to enum type using C#

Short introduction of enumeration type
Enumeration is consists of a set of named constants and their values. Below are the sample.

public enum ItemStatus { Delete=-1, InActive=0, Active=1 };

Above enumeration have three members: ItemStatus.Delete, ItemStatus.InActive, and ItemStatus.Active.

Now, we will check how to convert list of string to enum in C#.

List StatusList = new List();
StatusList.Add("Delete");
StatusList.Add("InActive");
StatusList.Add("Active");

For convert, we have to use below syntax.

var enumStatusList = StatusList.Select(x => Enum.Parse(typeof(ItemStatus), x)).Cast().ToList();

Now, you can access it directly in list object of ItemStatus.

List statusList = new List();
statusList = enumStatusList.ToList();

Top rated

no post found.