【发布时间】:2009-06-10 06:43:29
【问题描述】:
我有如下声明:
select new Action {
ParentContentType = action.ParentContentType != null ? (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType) : null
};
ParentContentType 是 ContentType 类型的可为空枚举
action.ParentContentType 映射到一个可以为空的 int 的数据库表。
如果 action.ParentContentType isnt null,我使用以下方法确定枚举值:
(ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType)
在 action.ParentContentType IS null 的情况下,我尝试将可空枚举设置为 null 值。
这没有编译,我得到:
Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between ContentType' and '<null>'
编辑
我可以创建一个空枚举值.. 即 ContentType.EMPTY。
但是:
ParentContentType = action.ParentContentType == null ? ContentType.EMPTY : (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType) };
也不行!
我得到了例外:
The argument 'value' was the wrong type. Expected 'Enums.ContentType'. Actual 'System.Object'.
【问题讨论】:
标签: linq-to-sql enums null