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#?
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)
csv.Split(',').Select(x => (Events)int.Parse(x)).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With