Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert CSV string to List<Enum>

Tags:

c#

generics

I have defined enum events:

public enum Events {
  UNLOCK = 1,
  LOCK = 2
}

as well as CSV string:

var csv = "1,2";

What would be preferable way to convert csv string to List< Events> in C#?

like image 833
user576700 Avatar asked Jul 19 '13 13:07

user576700


2 Answers

csv.Split(',').Select(s => (Events)Enum.Parse(typeof(Events), s));

BTW with generic enum class you can parse this way Enum<Events>.Parse(s) and whole code will look like:

csv.Split(',').Select(Enum<Events>.Parse)
like image 185
Sergey Berezovskiy Avatar answered Nov 10 '22 22:11

Sergey Berezovskiy


csv.Split(',').Select(x => (Events)int.Parse(x)).ToList();
like image 25
It'sNotALie. Avatar answered Nov 10 '22 21:11

It'sNotALie.