简体   繁体   中英

Mapping the value of the time field in LINQ to OData query

Recently I've been working with OData .Net Client. I'm using the DataServiceContext class as client. To get data I'm using LINQ to OData Query. The problem is when the LINQ maps value of time type (not datetime, but time) field f.ex. DocTime field:

The LINQ query:

 IQueryable<Document> query = CurrentServiceContainer.Orders.Where(e => e.DocTime >= new DateTime(2017, 1, 1, 12, 0, 0));

is mapped to the following OData query:

 "https://$HOST_ADDRESS$/b1s/v1/Orders()?$filter=DocTime ge datetime'2017-01-01T12:00:00'" (query.ToString())

, which execution gives the following result:

{
   "error" : {
      "code" : -1000,
      "message" : {
         "lang" : "en-us",
         "value" : "Query string error - the given value('datetime') of property 'DocTime' is of invalid time format"
      }
   }
}

As you can see the value of DocTime time field is mapped to " datetime'2017-01-01T12:00:00' ", which is wrong time format.

According to the documentation value of this field shoud be mapped to " '2017-01-01T12:00:00' " (without the "datetime" prefix). Is it possible to modify in any way ODataQuery (by configuration, software update or mayby manipulating URI) to get the desired result with no error?

Regarding your question if it is possible to modify the ODataQuery I see two options:

  1. [ClientSide] Register DataServiceContext_SendingRequest2 on the DataServiceContext you are using. SendingRequest2EventArgs contains the complete Request which is about to be sent. See here for an example which adds authentication headers to a data service context and which you could adapt to your needs.

  2. [ServerSide] Modify the ODataQueryOptions when they arrive at the server (which I described here ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM