开发者

How to handle a nullable DateTime in a LINQ-to-SQL where condition?

I need to select a list of coupons that have not expired yet. That is:

where Coupon.ExpiresOn > DateTimeUtc.Now

Problem is that Coupon.ExpiresOn is a nullable of DateTime (DateTime?). The code above does not return coupons with null values in Coupon.ExpiresOn. How could I change it to return null values? Thank you for yo开发者_运维百科ur help.


allow nulls in your where, like so:

where Coupon.ExpiresOn == null || Coupon.ExpiresOn > DateTimeUtc.Now


Add an or to your where statement:

where !Coupon.ExpiresOn.HasValue || Coupon.ExpiresOn.Value > DateTimeUtc.Now
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜