[英]How can I select a number and value from an enum in C#?
我刚刚更改了代码,现在它将一些数据存储在枚举而不是SQL Server表中。
这是枚举:
public enum ContentTypes : int
{
Article = 0,
Menu = 1,
ContentBlock = 3
}
我正在运行以下代码:
var contentTypes =
(
from contentType in this._contentTypeService.GetContentTypes()
select new
{
id = contentType.ContentTypeId,
name = contentType.Name
}
);
我该如何更改它,以便代替从内容服务中获取数据,而只查询枚举到contentTypes的数据?
我想你想要的是
var contentTypes =
from value in Enum.GetValues(typeof(ContentTypes)).Cast<int>()
select new
{
id = value,
name = Enum.GetName(typeof(ContentTypes), value)
};
pswg可能是对的,但我想你想要这样的东西 ?
这是更直接针对您问题的代码:
select new
{
id = (int)contentType,
name = contentType.ToString()
}
您可以简单地通过转换为int
来获取整数id并使用其ToString
来获取其名称(此处可能隐含或不隐含)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.