Thursday, April 28, 2011

How to select only Date value No Time in LINQ to SQL?

I need to have a query which list all the user according to the date value in the database. Problems is this, in the database the date format is 5/5/2009 4:30:12 but I want to compare with 5/5/2009. I think the value of the based date is 5/5/2009 12:00:00 and that's why I couldn't query it.

The query is something like

dim db = new databcontext dim user = from u in db.datacontext where u.signUpTime = 5/5/2009 select u.

May someone please check this problem.

Thanks in advance.

From stackoverflow
  • Your theory is correct. You should either use a range in your where clause:

    u.SignupTime >= new DateTime(2009,5,5,0,0,0) AndAlso _
    u.SignupTime < new DateTime(2009,5,6,0,0,0)
    

    or check only the date:

    u.SignupTime.Date = new DateTime(2009,5,5)
    
    kenny : I think I agree, the .Date property is the date/time at midnight.
    Andomar : +1 Maybe explain that SignupTime.Date returns only the date part of SignupTime
    Vicheanak : Thanks you so much. It works so perfect ..

0 comments:

Post a Comment