无法从“System.Guid”转换?到“System.Guid”
有谁知道如何处理这个错误?
cannot convert from 'System.Guid?' to 'System.Guid'
Does anyone know how to deal with this error?
cannot convert from 'System.Guid?' to 'System.Guid'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅 MSDN。
在这种情况下,只需使用
myNullableVariable.Value
(如果您确定它有值),或者使用(myNullableVariable.HasValue)?myNullableVariable.Value:somedefaulthere
如果您'不是。如果可空值实际上为 null 时不关心默认值是否为特定值,则还可以使用
GetValueOrDefault()
。最后一种方法是:
myNullableVariable.Value ??默认值
。看,从技术上讲,
MyType?
变量实际上是一个Nullable
。两者之间没有隐式或显式转换,因此您需要做的是手动从Nullable
中提取值。我列出的第三种方法是最简洁(也是最好的?)的方法,在大多数情况下这可能是最好的方法。See MSDN.
In that case, merely use
myNullableVariable.Value
(if you're sure it has a value), or(myNullableVariable.HasValue)?myNullableVariable.Value:somedefaulthere
if you're not.One can also use
GetValueOrDefault()
if one doesn't care if the default is a specific value when the nullable really is null.The last way to do it is this:
myNullableVariable.Value ?? defaultvalue
.See, technically a
MyType?
variable is aNullable<MyType>
under the covers. There are no implicit or explicit casts between the two, so what you need to do is extract the value out of theNullable
manually. The third way i listed is the most succinct (and best?) way to do it, to that would probably be best in most cases.使用 Guid?.Value 属性转换 'System.Guid?'到“系统.Guid”。如下例
Use Guid?.Value property to convert 'System.Guid?' to 'System.Guid'. as following example
只需在 if 语句中使用
Nullable.HasValue
来检查值是否为 null,然后在其后可以使用Nullable.Value
来获取某个值基础类型。Simply use
Nullable<T>.HasValue
in if statement to check the value for null, then after it you can useNullable<T>.Value
to gets the value of an underlying type.首先初始化guid变量。
然后设置您想要的值,例如:
First intialize the guid variable.
then set value in that which you want for eg:
无法从“System.Guid”转换?到“System.Guid”
您试图保存类型System.Guid?到类型系统.Guid
在你的模型中你可以编辑System.Guid吗?通过删除问号来更改为 System.Guid。
或者
RCIX 答案如上
cannot convert from 'System.Guid?' to 'System.Guid'
you are trying to save type System.Guid? to a type system.Guid
inside your model you can edit System.Guid? to System.Guid by removing the question mark.
or
RCIX answer above
如果您确定 value 不为 null,那么您可以将 value 转换为 (Guid)
或者您可以使用
或者另一种用法是
If you are sure value is not null then you can cast value as a (Guid)
Or you can use
Or another usage is