开发者

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

开发者 https://www.devze.com 2023-01-31 01:11 出处:网络
I need to select a list of coupons that have not expired yet. That is: where Coupon.ExpiresOn > DateTimeUtc.Now

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

精彩评论

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