Search code examples
c#objectquery

ObjectQuery, passing datetime in Where clause filter


How to pass in Date Time value here?

ObjectQuery<Item> _Query = ItemEntities.CreateQuery<Item>("Item");
_Query = _Query.Where("(it.StartDate >= 11/4/2009 5:06:08 PM)");

my sample code above does seem to work.

even with this

ObjectQuery<Item> _Query = ItemEntities.CreateQuery<Item>("Item");
_Query = _Query.Where("(it.StartDate >= \"11/4/2009 5:06:08 PM\")");

I got type cast error in EDM.


Solution

  • The following should work:

    ObjectQuery<Item> _Query = ItemEntities.CreateQuery<Item>("Item");
    _Query = _Query.Where("(it.StartDate >= DATETIME'11/4/2009 17:06:08')");
    

    See the documentation for more information on literals in ESQL queries.