我有以下语句:可空枚举(??)和LinqToSQL
select new Action {
ParentContentType = action.ParentContentType != null ? (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType) : null
};
ParentContentType是类型的ContentType
action.ParentContentType映射到这是一个可空INT数据库表的一个可为空的枚举。
如果action.ParentContentType 心不是空,我决定用枚举值:
(ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType)
在当action.ParentContentType IS空的情况下,我尝试将可空枚举的值设置为空。
这并不编译,我得到:
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'.
实际上,在这种情况下投射null会导致异常“无法翻译表达式”! – iasksillyquestions 2009-06-09 22:34:46
这很奇怪。上面的代码为我编译和运行。你能发布ParentContentType类型的定义吗? – 2009-06-10 16:50:25