REM*_*ESQ 2 asp.net-mvc c#-4.0
我正在尝试将一个另存enum为逗号分隔的列表保存在数据库中。
我知道我可以做这样的事情来实际存储逗号分隔的列表:
part.MyProperty = new[] {"foo", "bar"};
Run Code Online (Sandbox Code Playgroud)
并且数据库将有一个条目“ foo,bar”。
我不知道该怎么做是如何存储enum,例如:
public enum Choices { Choice1, Choice2, Choice3 }
Run Code Online (Sandbox Code Playgroud)
我收集到我必须使用Parse和ToString使用enum值的信息,但是我不确定该怎么做。
这似乎不正确:
part.MyProperty = new[] return from name in Enum.GetNames(typeof(T))
let enumValue = Convert.ToString((T)Enum.Parse(typeof(T), name, true))
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
这有什么错part.MyProperty = Enum.GetNames(typeof(Choices));?
要获取用逗号分隔的列表,请使用String.Join:
string csvEnums = string.Join(",", Enum.GetNames(typeof(Choices)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4006 次 |
| 最近记录: |