Date comparison not working in LINQ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leosuth
    New Member
    • Jul 2007
    • 5

    Date comparison not working in LINQ?

    Hi

    I've got the following function:

    Code:
       public static List<Order> FilterByDateRange(List<Order> _list,string _sdate, string _edate)
        {
          if (_list.Count == 0) return null;
          DateTime _start = Convert.ToDateTime(_sdate);
          string[] _ed = _edate.Split(' ');
          string _edate2 = _ed[0] + " 23:59:59";
          DateTime _end = Convert.ToDateTime(_edate2);
          List<Order> result = (from obj in _list
                                where
                                  (((DateTime)obj.DespatchDate >= _start) &&
                                  ((DateTime)obj.DespatchDate <= _end))
                                select obj).ToList();
          return result;
        }
    The idea being I supply a List of Orders, and I select all the Orders in that set that lie within a date range (note start date starts at 00:00:00 and end date ends at 23:59:59)

    I have three orders where obj.DespatchDat e equals
    {18/02/2010 18:03:01}
    {20/02/2010 14:01:51}
    {20/02/2010 14:02:03}

    I supply a date range that gives
    _start = {20/02/2010 00:00:00}
    _end = {22/02/2010 23:59:59}

    Problem: result always gets a Count = 0 where I would expect a Count of 2. I stepped through the code and very carefully checked the values of the various dates, so I know they are correct.

    Am I being incredibly stupid (certainly not an impossibility) or does date comparison not work in LINQ?

    PS. obj.DespatchDat e is type (DateTime?) - but that should not make any difference
  • leosuth
    New Member
    • Jul 2007
    • 5

    #2
    OK 'pologies all.

    It turned out that I WAS being stupid :)

    My Orders were actually in January and I was looking in February - so date comparison in Linq DOES work just fine!

    Comment

    Working...