2

I have implemented OData for querying the list generated by my API. But some times the list is empty and the OData Result should just return an empty json but it doesnt. Instead it throws up the following error

Value must be greater than or equal to 1. Parameter name: value Actual value was 0.

This is my controller method

 // Get states by Id
    [HttpGet]
    [ActionName("GetStatesByID")]    
    public ODataResult<DataAccess.Model.State> GetStatesByID(ODataQueryOptions options,int id)
    {

        var states = this.Repository.GetAllStatesByCountry(id).AsQueryable<DataAccess.Model.State>();

        var results = (options.ApplyTo(states) as IQueryable<DataAccess.Model.State>);


        long count = states.Count();

        return new ODataResult<DataAccess.Model.State>(results, null, count);

    }

I can put the count as 0 whenever the list is empty and that will not throw an error but that really is not a good practice, so i wanted to know if there is any other work around for this so that ODataResult returns an empty json when the list is empty rather than throwing up an error?

Thanks

Bitsian
  • 2,238
  • 5
  • 37
  • 72

2 Answers2

1

Have not had a chance to test this since I don't have a current environment, but give this a try:

[HttpGet]
[ActionName("GetStatesByID")]    
public ODataResult<DataAccess.Model.State> GetStatesByID(ODataQueryOptions options,int id)
{
    var states = this.Repository.GetAllStatesByCountry(id)
                                .AsQueryable<DataAccess.Model.State>();

    long count = states.Count();

    if(count == 0)
      return null;

    var results = (options.ApplyTo(states) as IQueryable<DataAccess.Model.State>);

    return new ODataResult<DataAccess.Model.State>(results, null, count);
}

Then handle the empty/null result client side, alternatively you could do >> this :

I.e. when count > 0 just serialize the ODataResult<> and return it as a Stream and when count is 0 you return a hardcoded string representation of the ODataResult (with a count of zero) etc. It's not as pretty but it could work.

Community
  • 1
  • 1
Tommy Grovnes
  • 4,126
  • 2
  • 25
  • 40
  • ya that would work but the client side guyz are asking for some consistency as all the other APIs were returning in the json format which contained "count" and "items" which contained the list. Is there a way that i can retain that format and send both count and items as null rather than just sending null? – Bitsian Nov 26 '12 at 10:08
  • 1
    Saw a couple of posts having the same problems as you have, i.e it's a limitation of the ODataResult class. Maybe you could do this http://stackoverflow.com/a/3079326/1045728 , i.e. when count > 0 just serialize the ODataResult<> and return it as a Stream (see link) and when count is 0 you return a hardcoded string representation of the ODataResult (with a count of zero) etc. It's not as pretty but it could work – Tommy Grovnes Nov 26 '12 at 10:46
0

This is a bug in the ODataResult class. The corresponding codeplex bug is at http://aspnetwebstack.codeplex.com/workitem/640

RaghuRam Nadiminti
  • 6,718
  • 1
  • 33
  • 30