Search code examples
asp.netwcfwcf-data-services

$filter option for DateTime in WCF Data Services query URL


WCF Data Services conveniently allow querying and filtering data via options in the URL. For example, say I want Products with a price greater than 20:

http://www.example.com/Service.svc/Products?$filter=Price gt 20

But how do I use the $filter option with a DateTime? Say I want all Products that have been modified this month.

http://www.example.com/Service.svc/Products?$filter=ModifiedDate gt '2012-05-02'

This doesn't work for me; it gives the error message

Operator 'gt' incompatible with operand types 'System.DateTime' and 'System.String' at position 13.

I get the same with other comparison operators (ge, lt, le, eq). What is going on? How do I make this work? Do I need a certain DateTime format? What I tried above seems to be documented here.


Solution

  • Hi try using the following syntax

    $filter=ModifiedDate gt datetime'2012-05-02T00:00:00'
    

    The other date time functions can be found in this reference for odata url conventions

    URI conventions :

    http://msdn.microsoft.com/en-us/library/dd728283.aspx