Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to filter records from DetailDB using todays Date
-------------
When i tried to filter in this way, i got 0 records.
C#
DetailDB.Where(x => x.DateWorked == DateTime.Now);

-----------
When i tried this way i got error:
nullable object must have a value

C#
DetailDB.Where(x => x.DateWorked.Value.Date == DateTime.Now.Date);

------------
Please help how do i filter it :(
Posted
Updated 24-Dec-15 1:21am
v2
Comments
CHill60 24-Dec-15 7:23am    
On your first attempt remember that DateTime.Now includes minutes, seconds and milliseconds. Try using DateTime.Now.Date instead (as you did on the second attempt)
[no name] 24-Dec-15 7:25am    
I tried DateTime.Now.Date
i got 0 records :(
CHill60 24-Dec-15 7:27am    
What type is DateWorked and do you actually have any records with today's date on them (you'd be surprised how many times that is an issue :-))
[no name] 24-Dec-15 7:29am    
DateWorked type is DateTime and yes i have records with todays date
Raje_ 24-Dec-15 7:31am    
Can you please try this and see if it works :

DetailDB.Where(x => x.DateWorked.Value.Date == null || x.DateWorked.Value.Date == DateTime.Now.Date);

1 solution

Try:
C#
var xx = DetailDB.Where(x => x.DateWorked == null ? false : ((DateTime)x.DateWorked).Date == DateTime.Now.Date);
 
Share this answer
 
Comments
[no name] 24-Dec-15 7:53am    
Thank you it worked :)
OriginalGriff 24-Dec-15 7:59am    
You're welcome!

I hate nullable value types, they always give me grief! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900