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. C#
  4. C# linq issue

C# linq issue

Scheduled Pinned Locked Moved C#
csharphelplinqquestion
4 Posts 4 Posters 0 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.
  • C Offline
    C Offline
    classy_dog
    wrote on last edited by
    #1

    I have a C# 2010 application that I would like to add the following lines of code:

    eDataContext rData = new eDataContext();
    DateTime complete_date = (DateTime)(from a in rData.cust
    where a.PkID == pkId
    select a.Complete_Date).FirstOrDefault();
    if (complete_date != null)

    The linq statement where DateTime complete_date is assigned a value works. However when I try to executed the line of code (complete_date != null), the program has a problem on this line of code. I found this out by stepping though the code. Thus can you tell me why the "if (complete_date != null)" line of code is having a problem? Also can you tell me what to do to solve the problem?

    P D J 3 Replies Last reply
    0
    • C classy_dog

      I have a C# 2010 application that I would like to add the following lines of code:

      eDataContext rData = new eDataContext();
      DateTime complete_date = (DateTime)(from a in rData.cust
      where a.PkID == pkId
      select a.Complete_Date).FirstOrDefault();
      if (complete_date != null)

      The linq statement where DateTime complete_date is assigned a value works. However when I try to executed the line of code (complete_date != null), the program has a problem on this line of code. I found this out by stepping though the code. Thus can you tell me why the "if (complete_date != null)" line of code is having a problem? Also can you tell me what to do to solve the problem?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      DateTime is not nullable. If you need to have a null date option, use DateTime? to declare it inside. This tells the compiler to create it as nullable.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      1 Reply Last reply
      0
      • C classy_dog

        I have a C# 2010 application that I would like to add the following lines of code:

        eDataContext rData = new eDataContext();
        DateTime complete_date = (DateTime)(from a in rData.cust
        where a.PkID == pkId
        select a.Complete_Date).FirstOrDefault();
        if (complete_date != null)

        The linq statement where DateTime complete_date is assigned a value works. However when I try to executed the line of code (complete_date != null), the program has a problem on this line of code. I found this out by stepping though the code. Thus can you tell me why the "if (complete_date != null)" line of code is having a problem? Also can you tell me what to do to solve the problem?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        FirstOrDefault is your clue. If not found, it will return the default value for DateTime which is DateTime.MinValue. Use:

        if (complete_date != DateTime.MinValue)

        Or

        if (complete_date != default(DateTime))

        instead. null is default for reference types such as classes and interfaces, for value types such as structs there is normally a static readonly field or property available such as MinValue or Empty, or you can use the parameterless constructor to get the default, or default(...) as above.

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        1 Reply Last reply
        0
        • C classy_dog

          I have a C# 2010 application that I would like to add the following lines of code:

          eDataContext rData = new eDataContext();
          DateTime complete_date = (DateTime)(from a in rData.cust
          where a.PkID == pkId
          select a.Complete_Date).FirstOrDefault();
          if (complete_date != null)

          The linq statement where DateTime complete_date is assigned a value works. However when I try to executed the line of code (complete_date != null), the program has a problem on this line of code. I found this out by stepping though the code. Thus can you tell me why the "if (complete_date != null)" line of code is having a problem? Also can you tell me what to do to solve the problem?

          J Offline
          J Offline
          John Brett
          wrote on last edited by
          #4

          DateTime is a ValueKind, so comparing it with null isn't very useful (it'll always not be null). If your LINQ query doesn't find any rows, then FirstOrDefault() will return a default instance of the requested data type (DateTime, in this case). There's a clue in the name ;) If you want to know if you found any rows or not, either test the query first (e.g. Any()) before evaluating First(), or compare the returned value with the default DateTime value (DateTime.MinValue, IIRC). If you do that however, you won't be able to tell the difference between no rows, and rows where the first had the minimum datetime value.

          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