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.
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
For DateTime filtering in oData feeds qualify the datetime variable with a DateTime prefix.
http://odata.netflix.com/v2/Catalog/Titles?$filter=DateModified eq DateTime'2012-01-31T09:45:16'
The above url shows how to filter the netflix odata feed based on date time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With