none
LINQ - DateTime Filter RRS feed

  • Question

  • I have not been able to successfully filter my queries with a date time. I am trying to select data in a date range and time window. For instance today, 8am to 5pm.

    P.S. One of my colums has a timestamp.

    Any help would be appreciated.


    “Somewhere someone is practicing, if you're not and you should meet, you will lose…”
    Monday, April 6, 2009 7:00 PM

Answers

  • It would help if you could post the query you are trying, along with relevant info about the data types you are trying to query. For example, something like the following should work:

     

     

    var q = from o in context.Orders
            where o.OrderDate > startDate && o.OrderDate < endDate
            select o;

     

     

    Where startDate and endDate are just DateTime variables.

    Also, if you can post more information about how it's not working, that would help as well. For example, if you're getting an exception, or the query just isn't returning the correct results, etc. If it's the wrong results, I would also suggest using the DataContext.Log property to log out the query that's getting executed. That might give you a clue about what is not working.

    Thanks,
    Sarah Parra


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by 01000011 Tuesday, April 7, 2009 1:46 AM
    Monday, April 6, 2009 10:12 PM
    Moderator

All replies

  • It would help if you could post the query you are trying, along with relevant info about the data types you are trying to query. For example, something like the following should work:

     

     

    var q = from o in context.Orders
            where o.OrderDate > startDate && o.OrderDate < endDate
            select o;

     

     

    Where startDate and endDate are just DateTime variables.

    Also, if you can post more information about how it's not working, that would help as well. For example, if you're getting an exception, or the query just isn't returning the correct results, etc. If it's the wrong results, I would also suggest using the DataContext.Log property to log out the query that's getting executed. That might give you a clue about what is not working.

    Thanks,
    Sarah Parra


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by 01000011 Tuesday, April 7, 2009 1:46 AM
    Monday, April 6, 2009 10:12 PM
    Moderator
  • If I understand your problem correctly, you should add hour at the end of you dateTime variable. Because the default time for a datetime variable = 12:00am. Therefore use the startDate.addHours(12).

    for example

    I want to select all today's records. So I can select them by filtering from my createdDate variable which is a time stamp (ie, 1/2/2009 1:23:00pm). 

    DateTime startDate = myCalendarPicker1.selectedDate;  // give you the date you selected with default time, ie 12/3/2009 12:00am
    DateTime endDate = myCalendarPicker2.selectedDate.AddHours(23.9999); // give you the date you selected with default time + hour you added, ie 12/3/2009 11:59pm

    var result = from t in tables.myRecords
                      where createdDate >= startDate && createdDate <= endDate
                      select t;

    Hope this help


    Jay




    Thursday, October 1, 2009 7:49 PM