WCF WebApi IQueryable doesn't work with DateTime properties
-
29-10-2019 - |
Question
I'm unable to query any object by the DateTime properties.
public class MyModel
{
public int Id { get; set; }
public DateTime TimeStamp { get; set; }
}
[ServiceContract]
public class BuildJobApi
{
[WebGet]
public IQueryable<MyModel> GetMyModels()
{
return _service.GetMyModels(); // IQueryable<MyModel>
}
}
I tried using WCF WebApi v0.5.0 and 0.6.0, both are throwing error messages.
Here's the URL generated (by WebApi) to query by Id (works):
/api/models?$filter=(Id eq 100)
Here's the URL generated to query by TimeStamp (doesn't work):
/api/models?$filter=TimeStamp ge DateTime'2012-02-22T00:00:00'
I can also query by date part (works):
/api/models?$filter=(year(ExceptionDateTime) eq 2012)
The exact error message is 500/Internal Server Error. With the message:
The server encountered an error processing the request. See server logs for more details.
Q: Is there anything that can be done to query the DateTime properties without querying each part separately?
Solution
Try to avoid serializing DateTime
, use string for this.
eg:
public class MyModel
{
public int Id { get; set; }
public string TimeStamp { get; set; }
}
new MyModel { Id = 123, TimeStamp = DateTime.Now.ToString("o") };
the "o"
is for the ISO 8601 format (culture invariant, stable format).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow