不能隐式转换System.DateTime类型?到System.DateTime [英] cannot implicitly convert type System.DateTime? to System.DateTime
本文介绍了不能隐式转换System.DateTime类型?到System.DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我执行以下操作时,我得到:
When I do the following I get:
inv.RSV = pid.RSVDate
我得到以下内容:不能隐式转换System.DateTime类型?到System.DateTime。
I get the following: cannot implicitly convert type System.DateTime? to System.DateTime.
在这种情况下,inv.RSV是DateTime,pid.RSVDate是DateTime?
In this case, inv.RSV is DateTime and pid.RSVDate is DateTime?
我尝试以下但不成功:
if (pid.RSVDate != null)
{
inv.RSV = pid.RSVDate != null ? pid.RSVDate : (DateTime?)null;
}
如果pid.RSVDate为null,我喜欢不分配inv.RSV的任何内容在这种情况下,它将为null。
If pid.RSVDate is null, I like to not assign inv.RSV anything in which case it will be null.
推荐答案
DateTime不能为空。它的默认值是 DateTime.MinValue
。
DateTime can't be null. It's default is DateTime.MinValue
.
您要做的是以下内容:
if (pid.RSVDate.HasValue)
{
inv.RSV = pid.RSVDate.Value;
}
或者更简洁:
inv.RSV = pid.RSVDate ?? DateTime.MinValue;
这篇关于不能隐式转换System.DateTime类型?到System.DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文