Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$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.

like image 417
Tom Hamming Avatar asked May 30 '12 22:05

Tom Hamming


2 Answers

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 :

  • prior to 5.0 (OData v2)
  • for 5.0 (OData v3)

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

like image 162
SCB Avatar answered Nov 14 '22 22:11

SCB


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.

like image 45
Vishnu Arunachalam Avatar answered Nov 14 '22 21:11

Vishnu Arunachalam