2
votes

I have an Entity Framework object returned by OData V4 controller. I return an IQueryable and if I call the OData endpoint without any OData clause I can succesfully do this:

var content = response.Content.ReadAsAsync<IQueryable<Person>>();

And the response in JSON is the following:

{
  "@odata.context":"http://xxx:8082/odata/$metadata#Persons","value":[
    {
      "Id":"291b9f1c-2587-4a35-993e-00033a81f6d5",
      "Active":true,
      "Alert":"Some alerts for the Person",
      "Comments":"Some comments for the Person"
    }
  ]
}

But as soon as I start to play with OData, for example by using $expand on a Complex property I get the following exception:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Linq.IQueryable`1[xxx.Person]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

And the response is the following:

{
  "@odata.context":"http://aqavnext01:8082/odata/$metadata#Persons","value":[
    {
      "Id":"291b9f1c-2587-4a35-993e-00033a81f6d5",
      "Active":true,
      "Alert":"Some alerts for the Person",
      "Comments":"Some comments for the Person",
      "Party":{
        "Id":"291b9f1c-2587-4a35-993e-00033a81f6d5"
      }
    }
  ]
}

And I am deserializing using the same object returned by my Web Api so I don't understand why it fails. Same issue when I apply $select.

1
Can you provide more code like the controller and data model? And maybe not directly related to your question, $expand is for navigation property but not complex type. - QianLi
I just send back a DbContext set. The problem is the deserialization in C#, in Javascript we have no problems at all. return dbContext.Set<Person>() - Raffaeu

1 Answers

6
votes

Try deserialize the content like this:

var content = response.Content.ReadAsAsync<ODataResponse<Person>>();

Where ODataResponse is:

internal class ODataResponse<T>
{
    public T[] Value { get; set; }
}