C# 选择日期,其中日期可以为空
我有一个像这样的对象:C# 选择日期,其中日期可以为空,c#,linq,C#,Linq,我有一个像这样的对象: public class MyObject { public Nullable<DateTime> SpecificDate {get;set;} ....other properties } 但是,当我写object.SpecificDate时。我没有智能感知来选择.Date属性 知道为什么吗 谢谢。您需要编写object.SpecificDate.Value.Date 但是,要小心,因为如果日期为null,则会引发错误。您可能需要检查TheObj
public class MyObject
{
public Nullable<DateTime> SpecificDate {get;set;}
....other properties
}
但是,当我写object.SpecificDate时。我没有智能感知来选择.Date属性
知道为什么吗
谢谢。您需要编写
object.SpecificDate.Value.Date
但是,要小心,因为如果日期为
null
,则会引发错误。您可能需要检查TheObject.SpecificDate!=首先为空。您需要检查SpecificDate.HasValue属性
因此,您的代码类似于:
if (condition){
TheQuery = from....
where TheObject.SpecificDate.HasValue && x.AppointDate.Date == TheObject.SpecificDate.Value.Date
}
什么是x.AppointDate.Date?如果它不能为null,你就会有问题。是的,这就是为什么我有If(condition)语句。
if (condition){
TheQuery = from....
where TheObject.SpecificDate.HasValue && x.AppointDate.Date == TheObject.SpecificDate.Value.Date
}