选择日期可以为空的日期
我有一个如下所示的对象:
public class MyObject
{
public Nullable<DateTime> SpecificDate {get;set;}
....other properties
}
我正在编写一个动态查询,该查询接收该对象作为参数,并且我可能需要也可能不需要 SpecificDate:
if (condition){
TheQuery = from....
where x.AppointDate.Date == TheObject.SpecificDate.Date
}
但是,当我编写 TheObject.SpecificDate 时。我没有得到智能感知来选择 .Date 属性。
知道为什么吗?
谢谢。
I have an object that looks like this:
public class MyObject
{
public Nullable<DateTime> SpecificDate {get;set;}
....other properties
}
I'm writing a dynamic query that receive this object as a parameter and where I may or may not need the SpecificDate:
if (condition){
TheQuery = from....
where x.AppointDate.Date == TheObject.SpecificDate.Date
}
However, when I write TheObject.SpecificDate. I'm not getting the intellisense to choose the .Date property.
Any idea why?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要编写
TheObject.SpecificDate.Value.Date
。但是,请小心,因为如果日期为
null
,则会抛出异常。您可能需要首先检查TheObject.SpecificDate != null
。You need to write
TheObject.SpecificDate.Value.Date
.However, be careful because if the date is
null
this will throw. You might want to check thatTheObject.SpecificDate != null
first.您需要检查 SpecificDate.HasValue 属性
因此您的代码将类似于:
You need to check the SpecificDate.HasValue property
So your code would be something like: