可空枚举 (??) 和 LinqToSQL

发布于 2024-07-23 08:18:32 字数 1181 浏览 4 评论 0原文

我有以下声明:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法少女 2024-07-30 08:18:32

我会同意您的 ContentType.NullContentType.Empty 想法,否则您将在整个应用程序中检查空值...加上 ContentType.Empty< /code> 更具描述性。

I would go with your idea of ContentType.Null or ContentType.Empty otherwise you will be checking for nulls all throughout your application... Plus ContentType.Empty is more descriptive.

天气好吗我好吗 2024-07-30 08:18:32

奇怪的是:

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'.

WTF?

Oddly:

ParentContentType = action.ParentContentType == null ? ContentType.EMPTY : (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType) 

results in an exception:

The argument 'value' was the wrong type. Expected 'Enums.ContentType'. Actual 'System.Object'.

WTF?

厌倦 2024-07-30 08:18:32

null 是无类型的。 您必须显式地转换它,因为 ? C# 中的运算符要求第二个参数必须与第一个参数具有完全相同的类型(或隐式可转换)。

因为两者必须是相同的类型,并且 null 不能转换为值类型,所以它们都必须是可以为 null 的类型:

select new Action {
  ParentContentType = action.ParentContentType != null ?
    (ContentType?)Enum.ToObject(typeof(ContentType), action.ParentContentType) :
    (ContentType?)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:

select new Action {
  ParentContentType = action.ParentContentType != null ?
    (ContentType?)Enum.ToObject(typeof(ContentType), action.ParentContentType) :
    (ContentType?)null 
};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文