Store enum as comma separated list

asp.net-mvc, c#-4.0

Solution

What's wrong with `part.MyProperty = Enum.GetNames(typeof(Choices));` ?

To get a comma separated list, use `String.Join`:

string csvEnums = string.Join(",", Enum.GetNames(typeof(Choices)));

Problem

I am trying to save an `enum` as a comma-separated list in a DB. I know I can do something like this to actually store a comma separated list: ``` part.MyProperty = new[] {"foo", "bar"}; ``` and the DB would have an entry "foo, bar". What I don't know how to do is how to store an `enum`, such as: ``` public enum Choices { Choice1, Choice2, Choice3 } ``` I gather I have to use `Parse` and `ToString` to use the `enum` values, but I am not sure how to do it. This doesn't seem right: ``` part.MyProperty = new[] return from name in Enum.GetNames(typeof(T)) let enumValue = Convert.ToString((T)Enum.Parse(typeof(T), name, true)) ``` Any thoughts?

Original source