无法从“System.Guid”转换?到“System.Guid”

发布于 2024-08-16 08:37:14 字数 98 浏览 2 评论 0原文

有谁知道如何处理这个错误?

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

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

发布评论

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

评论(6

如果没有你 2024-08-23 08:37:14

请参阅 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 a Nullable<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 the Nullable 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.

2024-08-23 08:37:14

使用 Guid?.Value 属性转换 'System.Guid?'到“系统.Guid”。如下例

Obj GetValue(Guid yourID)
{
return FetchObject(yourID)
}
Void main()
{
Guid? passvalue;
Obj test = GetValue(passvalue.Value);
}

Use Guid?.Value property to convert 'System.Guid?' to 'System.Guid'. as following example

Obj GetValue(Guid yourID)
{
return FetchObject(yourID)
}
Void main()
{
Guid? passvalue;
Obj test = GetValue(passvalue.Value);
}
救星 2024-08-23 08:37:14

只需在 if 语句中使用 Nullable.HasValue 来检查值是否为 null,然后在其后可以使用 Nullable.Value 来获取某个值基础类型。

int? a = null;
if(a.HasValue){
  Console.WriteLine($"a has value {a.Value}");
} else {
  Console.WriteLine("No value found in a");
}

Simply use Nullable<T>.HasValue in if statement to check the value for null, then after it you can use Nullable<T>.Value to gets the value of an underlying type.

int? a = null;
if(a.HasValue){
  Console.WriteLine(
quot;a has value {a.Value}");
} else {
  Console.WriteLine("No value found in a");
}
陪你到最终 2024-08-23 08:37:14

首先初始化guid变量。

Guid yourGuid= Guid.NewGuid()

然后设置您想要的值,例如:

 Guid defaultId = Guid.NewGuid();
 if (customerRow.GuardianState.Length > 2) {
 Guid StateId = record.StateId ?? defaultId;}

First intialize the guid variable.

Guid yourGuid= Guid.NewGuid()

then set value in that which you want for eg:

 Guid defaultId = Guid.NewGuid();
 if (customerRow.GuardianState.Length > 2) {
 Guid StateId = record.StateId ?? defaultId;}
森林很绿却致人迷途 2024-08-23 08:37:14

无法从“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

浪荡不羁 2024-08-23 08:37:14

如果您确定 value 不为 null,那么您可以将 value 转换为 (Guid)

var identifier = (Guid)myOtherIdentifier!;

或者您可以使用

var identifier = myOtherIdentifier.HasValue ? myOtherIdentifier : Guid.Empty;

或者另一种用法是

var identifier = myOtherIdentifier ?? Guid.Empty;

If you are sure value is not null then you can cast value as a (Guid)

var identifier = (Guid)myOtherIdentifier!;

Or you can use

var identifier = myOtherIdentifier.HasValue ? myOtherIdentifier : Guid.Empty;

Or another usage is

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