I have a fairly large data model that I want to expose using Web API OData using the OData V4 protocol.
The underlying data is stored in a SQL Server 2012 database.
For those who are using older versions of OData without Datetime support, you can simply use the .AddQueryOption extension to include the datetime filters manually...
Example - If you had a query defined as follows:
var date1 = new DateTime();
var query = from a in service.Entity
where a.DateField = date1
select a;
This would not give you the result you expect b/c the translated query would effectively be something like https://test.com/Entity?$filter=DateField eq 2020-02-24T00:00:00Z. When it gets to the server this wont execute the expected query b/c of the datetime offset.
to get around this do the following:
var date1 = new DateTime().ToString("yyyy-MM-dd");
var filters = "date(DateField) eq " + date1;
var query = from a in service.Entity.AddQueryOption("$filter", filters);
This will allow you to query against odata using datetime. Now, all you need to do is handle the POST, PUT, DELETE commands.
To do this, simply ensure that you have the timezone information or the client offset serialized in the header of the request. Use this in the web api to adjust the dates accordingly. I generally will add an extension method that is used in a custom serializer to adjust the dates when the payload is deserialized.
Alos, be sure to write such that the newer version of web api that handle timezone information properly will continue to function as expected...