Data check in query
-
Hi, In the where clause checking to make sure that the question had not expired. But this is not a required field and sometimes left blank. How to handle to blank condition? I should still show the question, since it never expires. Can I add 10 years on the fly to this blank condition in LINQ? Much appreciated! thanks.
var x = (from dq in db.Questions
where dq.Enabled.Equals(true) &&
(dq.ExpirationDate == null) ? DateTime.Now.AddYears(10) : dq.ExpirationDate >= DateTime.Now -
Hi, In the where clause checking to make sure that the question had not expired. But this is not a required field and sometimes left blank. How to handle to blank condition? I should still show the question, since it never expires. Can I add 10 years on the fly to this blank condition in LINQ? Much appreciated! thanks.
var x = (from dq in db.Questions
where dq.Enabled.Equals(true) &&
(dq.ExpirationDate == null) ? DateTime.Now.AddYears(10) : dq.ExpirationDate >= DateTime.NowvkEE wrote:
var x = (from dq in db.Questions where dq.Enabled.Equals(true) && (dq.ExpirationDate == null) ? DateTime.Now.AddYears(10) : dq.ExpirationDate >= DateTime.Now
In a word: yes here's how I'd write it using the "Null coalescing operator": (see The C# ?? null coalescing operator (and using it with LINQ)[^]
var x = (from dq in db.Questions
where dq.Enabled &&
(dq.ExpirationData ?? DateTime.Now.AddYears(10)) >= DateTime.Now)If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein