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. Web Development
  3. ASP.NET
  4. nullable dateTime linq query issue

nullable dateTime linq query issue

Scheduled Pinned Locked Moved ASP.NET
databasehelpcsharplinqjson
5 Posts 3 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.
  • M Offline
    M Offline
    miss786
    wrote on last edited by
    #1

    Hi all, I am trying to run a query which can allow to output 'UploadDate' value only. This is what I currently have and i keep getting a -- Cannot implicitly convert type 'System.DateTime?' to 'API_09.database_BD'-- error, on the following code:

    public database_BD GetDate()
    {
    var data = db.database_BD.Select(d => d.UploadDate)
    .OrderByDescending(c => c)
    .FirstOrDefault();

        return data; // error line.
    }
    

    Database_BD Entity framework class:

    public partial class database_BD
    {
    public Nullable UploadDate { get; set; }
    public string uUsername { get; set; }
    }

    any suggestions and help would be most welcome.

    L Richard DeemingR 2 Replies Last reply
    0
    • M miss786

      Hi all, I am trying to run a query which can allow to output 'UploadDate' value only. This is what I currently have and i keep getting a -- Cannot implicitly convert type 'System.DateTime?' to 'API_09.database_BD'-- error, on the following code:

      public database_BD GetDate()
      {
      var data = db.database_BD.Select(d => d.UploadDate)
      .OrderByDescending(c => c)
      .FirstOrDefault();

          return data; // error line.
      }
      

      Database_BD Entity framework class:

      public partial class database_BD
      {
      public Nullable UploadDate { get; set; }
      public string uUsername { get; set; }
      }

      any suggestions and help would be most welcome.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hello miss, Could you please specify the return type of

      public -- database_BD GetDate()

      1 Reply Last reply
      0
      • M miss786

        Hi all, I am trying to run a query which can allow to output 'UploadDate' value only. This is what I currently have and i keep getting a -- Cannot implicitly convert type 'System.DateTime?' to 'API_09.database_BD'-- error, on the following code:

        public database_BD GetDate()
        {
        var data = db.database_BD.Select(d => d.UploadDate)
        .OrderByDescending(c => c)
        .FirstOrDefault();

            return data; // error line.
        }
        

        Database_BD Entity framework class:

        public partial class database_BD
        {
        public Nullable UploadDate { get; set; }
        public string uUsername { get; set; }
        }

        any suggestions and help would be most welcome.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        If you only want to return the upload date, change the return type of your GetDate method to DateTime? and everything should work:

        public DateTime? GetDate()
        {
        return db.database_BD.Select(d => d.UploadDate)
        .OrderByDescending(c => c)
        .FirstOrDefault();
        }

        If the return type needs to be database_BD, then you'll need to return that type:

        public database_BD GetDate()
        {
        var data = db.database_BD.Select(d => d.UploadDate)
        .OrderByDescending(c => c)
        .FirstOrDefault();

        return new database\_BD { UploadDate = data };
        

        }

        If you want the full details of the latest record, then you'll need to change your query:

        public database_BD GetDate()
        {
        return db.database_BD
        .OrderByDescending(d => d.UploadDate)
        .FirstOrDefault();
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        M 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          If you only want to return the upload date, change the return type of your GetDate method to DateTime? and everything should work:

          public DateTime? GetDate()
          {
          return db.database_BD.Select(d => d.UploadDate)
          .OrderByDescending(c => c)
          .FirstOrDefault();
          }

          If the return type needs to be database_BD, then you'll need to return that type:

          public database_BD GetDate()
          {
          var data = db.database_BD.Select(d => d.UploadDate)
          .OrderByDescending(c => c)
          .FirstOrDefault();

          return new database\_BD { UploadDate = data };
          

          }

          If you want the full details of the latest record, then you'll need to change your query:

          public database_BD GetDate()
          {
          return db.database_BD
          .OrderByDescending(d => d.UploadDate)
          .FirstOrDefault();
          }


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          miss786
          wrote on last edited by
          #4

          Dear Richard, Your first solution worked. I did not realize, i was missing something so silly. I appreciate your time and help. Many thanks.

          Richard DeemingR 1 Reply Last reply
          0
          • M miss786

            Dear Richard, Your first solution worked. I did not realize, i was missing something so silly. I appreciate your time and help. Many thanks.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            No problem - we've all had days like that! :)


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            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