locked
Compare dates in entity query RRS feed

  • Question

  • Hello !

    i have problems in comparing dates on entity query.This is my code

    Dim d as date=Cdate("17/01/2011")

    dim Query=From t1 in context.mytable where t1.dt < d

                      select t1

     

    But this query shows the right results only when  the year on d is greater than year on t1.dt

    if  the year on both dates are the same , the records are not in the query results.

    For example if i have 2 records :

    1. dt=13/01/2011"   -    this record isn't on query results  ( but should be)

    2.dt=22/12/2010  -    this record is on query result.

     

    what can i do ?

    Thank you.

     

    Thursday, December 22, 2011 2:51 AM

Answers

  • Hi dcode25;

    I just tried it on my system and it worked in both forms with no issues. I did have to change the date in

    Dim d As Date = CDate("17/01/2011")

    to this

    Dim d As Date = CDate("01/17/2011")

    But that may have something to do with culture that the two machines are using. Seeming that Entity Framework is using the DateTime data type on your system I also tried it like this with no issues.

    Dim d As DateTime = CDate("01/17/2011")

    I can not see any reason why it is not working on yours.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Allen_MSDN Tuesday, December 27, 2011 1:30 PM
    • Marked as answer by Allen_MSDN Wednesday, December 28, 2011 1:34 AM
    Thursday, December 22, 2011 6:43 PM

All replies

  • Hi dcode25;

    A couple of question. 1 how is the date column in the database defined what is its data type? 2 How is the data type in the model / code for that column in the database?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, December 22, 2011 3:09 AM
  • in the database , the data type of the column "dt" is : Date

    in the model , the data type is :DateTime

    Thursday, December 22, 2011 5:32 PM
  • Hi dcode25;

    I just tried it on my system and it worked in both forms with no issues. I did have to change the date in

    Dim d As Date = CDate("17/01/2011")

    to this

    Dim d As Date = CDate("01/17/2011")

    But that may have something to do with culture that the two machines are using. Seeming that Entity Framework is using the DateTime data type on your system I also tried it like this with no issues.

    Dim d As DateTime = CDate("01/17/2011")

    I can not see any reason why it is not working on yours.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Allen_MSDN Tuesday, December 27, 2011 1:30 PM
    • Marked as answer by Allen_MSDN Wednesday, December 28, 2011 1:34 AM
    Thursday, December 22, 2011 6:43 PM
  • change your date to "yyyyMMdd" format

    Dim d as date=Cdate("17/01/2011") would be Dim d as date=Cdate("20110117")

    Friday, December 23, 2011 11:48 AM
  • I think you have to compare days , months and years 

    and dont compare the whole object of the datetime data type

    Sunday, December 25, 2011 12:04 PM