我有以下語句:可空枚舉(??)和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