Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Data check in query

Data check in query

Scheduled Pinned Locked Moved LINQ
databasequestioncsharplinqtutorial
2 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vkEE
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • V vkEE

      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

      T Offline
      T Offline
      TheGreatAndPowerfulOz
      wrote on last edited by
      #2

      vkEE 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups