0

我有一个枚举:-

    public enum EnumType
    {
        Type1_Template,
        Type2_Folders,
        Type3_Template,
        Type1_Folders,
    }

现在,在我想要的控制器中

  1. 枚举列表和
  2. 将 _ 下划线替换为空格。

所以为此:-获取我拥有的枚举列表

return new Models.DTOObject()
            {
                ID = model.id,
                Name = model.Name,
                Description = model.Description,
                //Type is the property where i want the List<Enum> and replace the underscore with space
                Type = Enum.GetValues(typeof(EnumType)).Cast<EnumType>().ToList()
            };

但是现在,我正在尝试这样的事情(虽然听起来可能很奇怪):-

return new Models.Customers()
            {
                ID = model.id,
                Name = model.Name,
                Description = model.Description,
                //Type is the property where i want the List<Enum> and replace the underscore with space
                Type = Enum.GetValues(typeof(EnumType)).Cast<EnumType>().ToList().Select(e => new
                {
                    Value = e,
                    Text = e.ToString().Replace("_", " ")
                })
            };

但会引发语法错误(';' missing )。虽然它只是被试了一下。请让我知道我该如何实现它。

4

2 回答 2

8

你应该能够做到

Enum.GetNames(typeof(EnumType)).Select(item => item.Replace('_',' '));
于 2013-02-26T07:37:58.453 回答
0

你应该使用

string[] names = Enum.GetNames(typeof(EnumType));

之后,您可以使用 for 循环(或类似的东西)并将“_”替换为“”。

for(int i = 0; i < names.Length; i++){
   names[i].Replace('_',' ');
}

MSDN

于 2013-02-26T07:38:32.337 回答