Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq where clause with multiple conditions and null check

I'm trying to check if a date is null in linq and if it isn't check its a future date.

QuestionnaireRepo.FindAll(q => !q.ExpiredDate.HasValue || q.ExpiredDate >DateTime.Now).OrderByDescending(order => order.CreatedDate);

I need the second check to only apply if the first is true. I am using a single repository pattern and FindAll accepted a where clause

ANy ideas? There are lots of similar questions on here but not that give the answer, I'm very new to Linq as you may of guessed :)

Edit: I get the results I require now but it will be checking the > conditional on null values in some cases. Is this not a bad thing?

like image 651
Andrew Avatar asked Mar 17 '10 16:03

Andrew


1 Answers

Won't this work?

QuestionnaireRepo.FindAll(
    q => (q.ExpiredDate == null) || (q.ExpiredDate > DateTime.Now));
like image 98
LukeH Avatar answered Nov 13 '22 22:11

LukeH