可空枚举 (??) 和 LinqToSQL
我有以下声明:
select new Action {
ParentContentType = action.ParentContentType != null ? (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType) : null
};
ParentContentType 是 ContentType action 类型的可为 null 枚举。ParentContentType
映射到一个可为 null 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'.
I have the following statement:
select new Action {
ParentContentType = action.ParentContentType != null ? (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType) : null
};
ParentContentType is a nullable enum of type ContentType
action.ParentContentType maps to a database table which is a nullable int.
If action.ParentContentType isnt null, I determine the enum value using:
(ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType)
In the case of when action.ParentContentType IS null I try to set the nullable enum to the value null.
This doesnt compile and I get:
Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between ContentType' and '<null>'
EDIT
I could create a null enum value.. ie ContentType.EMPTY.
However:
ParentContentType = action.ParentContentType == null ? ContentType.EMPTY : (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType)
};
Doesnt work either!
I get the exception:
The argument 'value' was the wrong type. Expected 'Enums.ContentType'. Actual 'System.Object'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会同意您的
ContentType.Null
或ContentType.Empty
想法,否则您将在整个应用程序中检查空值...加上ContentType.Empty< /code> 更具描述性。
I would go with your idea of
ContentType.Null
orContentType.Empty
otherwise you will be checking for nulls all throughout your application... PlusContentType.Empty
is more descriptive.奇怪的是:
导致异常:
WTF?
Oddly:
results in an exception:
WTF?
null
是无类型的。 您必须显式地转换它,因为 ? C# 中的运算符要求第二个参数必须与第一个参数具有完全相同的类型(或隐式可转换)。因为两者必须是相同的类型,并且
null
不能转换为值类型,所以它们都必须是可以为 null 的类型:但是,这是相当模糊的。 我从来没有想过你可以创建一个可为空的枚举(我想你可以,因为你发布了这个问题——我从未尝试过)。
正如您所建议的,您可能会更好地使用意味着“无”的枚举值。 对于大多数开发人员来说,这并不奇怪。 您只是不希望
enum
可以为空。null
is untyped. You have to cast it explicitly because the ? operator in C# requires that the second argument must be of the exact same type (or implicitly convertible) as the first.Because the two must be of the same type, and
null
cannot be cast to a value type, they both must of the nullable type:However, this is pretty obscure. It never even occurred to me that you could create a nullable of an enum (I guess you can, since you posted the question -- I've never tried).
You will probably be better off with an enum value that means "nothing", as you suggested. That would be less surprising to most developers. You just don't expect an
enum
to be nullable.