Gluon
Gluon

Reputation: 91

Compare date field with today's date if date field is not null

I want to query for data from a db that based on a date field i have called ExpiryDate. I want to get ALL data (ExpiryDate may be null) EXCEPT values with an ExpiryDate which is more than a day ago from today's date.

This is my script:

from a in b
where a.EXPIRYDATE == null || a.EXPIRYDATE != null 
&& a.EXPIRYDATE >= DateTime.Today.AddDays(-1)
select new ...

Can anyone explain why doesn't it work?

By the way, this script works:

from a in b
where a.EXPIRYDATE == null || a.EXPIRYDATE != null 
&& a.EXPIRYDATE >= DateTime.Today
select new ...

The EXPIRYDATE fields are as follows:

05-MAY-18

07-SEP-17

22-NOV-17

05-AUG-17

18-APR-18

02-FEB-18

28-AUG-17

31-DEC-99

01-DEC-90

31-DEC-99

31-DEC-99

31-DEC-99

31-DEC-99

31-DEC-99

31-DEC-99

31-DEC-17

31-DEC-99

(null)

(null)

(null)

(null)

(null)

(null)

(null)

(null)

(null)

(null)

(null)

(null)

(null)

(null)

(null)

Upvotes: 0

Views: 129

Answers (1)

Gluon
Gluon

Reputation: 91

Apologies all, it is regarding LINQ query. My question was to get data for all fields except when the expirydate is before yesterday.

I have found the solution:

DateTime yest = DateTime.Today.AddDays(-1);
returnValue = (from a in b
where (a.EXPIRYDATE == null ||  a.EXPIRYDATE > yest)
select new ...

I have to substract the day from today and store it in a variable before the LINQ query instead of using DateTime.Today.AddDays(-1) inside the query.

Upvotes: 3

Related Questions