By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

Date comparison not working in LINQ?

P: 5
Hi

I've got the following function:

Expand|Select|Wrap|Line Numbers
  1.    public static List<Order> FilterByDateRange(List<Order> _list,string _sdate, string _edate)
  2.     {
  3.       if (_list.Count == 0) return null;
  4.       DateTime _start = Convert.ToDateTime(_sdate);
  5.       string[] _ed = _edate.Split(' ');
  6.       string _edate2 = _ed[0] + " 23:59:59";
  7.       DateTime _end = Convert.ToDateTime(_edate2);
  8.       List<Order> result = (from obj in _list
  9.                             where
  10.                               (((DateTime)obj.DespatchDate >= _start) &&
  11.                               ((DateTime)obj.DespatchDate <= _end))
  12.                             select obj).ToList();
  13.       return result;
  14.     }
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.DespatchDate 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.DespatchDate is type (DateTime?) - but that should not make any difference
Feb 22 '10 #1
Share this Question
Share on Google+
1 Reply


P: 5
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!
Feb 22 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.