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. How to write a Linq 'Where' clause for nullable datetime?

How to write a Linq 'Where' clause for nullable datetime?

Scheduled Pinned Locked Moved ASP.NET
databasecsharplinqjsonhelp
7 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

    Dear all, I am writing to seek help in writing a linq 'Where' clause to get results between two dates. I am using the following method below, which is currently showing an empty string.

        \[HttpGet\]
        public IEnumerable GetDate(DateTime? start, DateTime? end)
        {
    
            var data = from c in db.database\_WICs
                       where c.UploadDate == start &&
                             c.UploadDate == end
                       select c;
            return data.ToList();
        }
    

    http://localhost:45361/api/data?start=25/11/2013&end=28/11/2013 Any help would be very much appreciated. Many thanks

    C Richard DeemingR 2 Replies Last reply
    0
    • M miss786

      Dear all, I am writing to seek help in writing a linq 'Where' clause to get results between two dates. I am using the following method below, which is currently showing an empty string.

          \[HttpGet\]
          public IEnumerable GetDate(DateTime? start, DateTime? end)
          {
      
              var data = from c in db.database\_WICs
                         where c.UploadDate == start &&
                               c.UploadDate == end
                         select c;
              return data.ToList();
          }
      

      http://localhost:45361/api/data?start=25/11/2013&end=28/11/2013 Any help would be very much appreciated. Many thanks

      C Offline
      C Offline
      cdpsource
      wrote on last edited by
      #2

      try this

      [HttpGet]
      public IEnumerable GetDate(DateTime? start, DateTime? end)
      {

              var data = from c in db.database\_WICs
                         where c.UploadDate == (start==null? c.UploadDate : start) &&
                               c.UploadDate == (end=null? c.UploadDate : end)
                         select c;
              return data.ToList();
          }
      
      M 1 Reply Last reply
      0
      • M miss786

        Dear all, I am writing to seek help in writing a linq 'Where' clause to get results between two dates. I am using the following method below, which is currently showing an empty string.

            \[HttpGet\]
            public IEnumerable GetDate(DateTime? start, DateTime? end)
            {
        
                var data = from c in db.database\_WICs
                           where c.UploadDate == start &&
                                 c.UploadDate == end
                           select c;
                return data.ToList();
            }
        

        http://localhost:45361/api/data?start=25/11/2013&end=28/11/2013 Any help would be very much appreciated. Many thanks

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

        miss786 wrote:

        get results between two dates

        miss786 wrote:

        c.UploadDate == start && c.UploadDate == end

        The query you've written will only return records where the UploadDate is equal to the start parameter AND equal to the end parameter. Unless you pass the same value for both parameters, this condition will never be met. Try something like this:

        var data = db.databases_WICs;

        if (start != null)
        {
        data = data.Where(c => c.UploadDate >= start);
        }
        if (end != null)
        {
        data = data.Where(c => c.UploadDate <= end);
        }

        return data.ToList();


        "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
        • C cdpsource

          try this

          [HttpGet]
          public IEnumerable GetDate(DateTime? start, DateTime? end)
          {

                  var data = from c in db.database\_WICs
                             where c.UploadDate == (start==null? c.UploadDate : start) &&
                                   c.UploadDate == (end=null? c.UploadDate : end)
                             select c;
                  return data.ToList();
              }
          
          M Offline
          M Offline
          miss786
          wrote on last edited by
          #4

          Thank you so much for your response. I tried your code and I am sorry to inform, I got the following error "Cannot convert null to 'bool' because it is a non-nullable value type" Any help would be very much appreciated.

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            miss786 wrote:

            get results between two dates

            miss786 wrote:

            c.UploadDate == start && c.UploadDate == end

            The query you've written will only return records where the UploadDate is equal to the start parameter AND equal to the end parameter. Unless you pass the same value for both parameters, this condition will never be met. Try something like this:

            var data = db.databases_WICs;

            if (start != null)
            {
            data = data.Where(c => c.UploadDate >= start);
            }
            if (end != null)
            {
            data = data.Where(c => c.UploadDate <= end);
            }

            return data.ToList();


            "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
            #5

            I really appreciate your time and help. I am currently experiencing error: Cannot implicitly convert type:'System.Linq.IQueryable' to 'System.Data.Entity.DbSet'. An explicit conversion exists (are you missing a cast?) on the following lines of code:

            if (start != null)
            {
            data = data.Where(c => c.UploadDate >= start);
            }
            if (end != null)
            {
            data = data.Where(c => c.UploadDate <= end);
            }

            I tried adding FirstOrDefault() to the code but I am sorry to inform that I can not figure out the reason for the issue above. Thank you

            Richard DeemingR 1 Reply Last reply
            0
            • M miss786

              I really appreciate your time and help. I am currently experiencing error: Cannot implicitly convert type:'System.Linq.IQueryable' to 'System.Data.Entity.DbSet'. An explicit conversion exists (are you missing a cast?) on the following lines of code:

              if (start != null)
              {
              data = data.Where(c => c.UploadDate >= start);
              }
              if (end != null)
              {
              data = data.Where(c => c.UploadDate <= end);
              }

              I tried adding FirstOrDefault() to the code but I am sorry to inform that I can not figure out the reason for the issue above. Thank you

              Richard DeemingR Online
              Richard DeemingR Online
              Richard Deeming
              wrote on last edited by
              #6

              Try either:

              var data = db.databases_WICs.AsQueryable();

              or:

              IQueryable<database_WICs> data = db.databases_WICs;


              "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

                Try either:

                var data = db.databases_WICs.AsQueryable();

                or:

                IQueryable<database_WICs> data = db.databases_WICs;


                "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
                #7

                Thanks Richard once again, you have been a great help. I manage to get the issue resolved and the problem was in date converter code that was in the Global.asax file which was not allowing the date parameters to pass through the URL query string. Thank you so much for your help and time into this issue. I really appreciate it.

                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