Search code examples
c#wcfodata

How to Find all data of the day by the WCF Odata filter


We want to date filter criteria for the getting all data of the day or today. We are trying code as below but we are getting error. Please provide need full solution.

$filter=ServiceCallID eq 425977 and DateCreated eq '11/28/2016 12:45:25 PM'

Error is "Operator 'eq' incompatible with operand types 'System.DateTime' and 'System.String' at position 40." And We are using v2 version of odata.


Solution

  • I believe you need to define the parameter as a DateTime. You get the error because .NET interprets your parameter as a string, but the type is DateTime.

    From odata.org:

    datetime'yyyy-mm-ddThh:mm[:ss[.fffffff]]' NOTE: Spaces are not allowed between datetime and quoted portion. datetime is case-insensitive

    Try this:

    $filter=ServiceCallID eq 425977 and DateCreated eq datetime'2016-11-18T12:45:25'
    

    Note that I also changed the format to YYYY-MM-dd. You may also want to change eq to gt or lt, depending on your definition of today.

    So your final solution may be greater or equal to 2016-11-18. And less than 2016-11-19. It should give all items during 2016-11-18.

    $filter=ServiceCallID eq 425977 and DateCreated ge datetime'2016-11-18' and DateCreated lt datetime'2016-11-19'