Search code examples
c#asp.net-web-apiodataiqueryable

using the OData Queryable with a repository?


I have this repository method:

public IQueryable<TModel> GetAll()
{
    using (var context = new DatabaseContext())
    {
        return context.Set<TModel>().AsQueryable();
    }
}

where TModel is an artist-model.. anyway

And then I have this action in my controller:

// GET api/artist
[Queryable]
public IQueryable<ArtistModel> Get()
{
    return _repo.GetAll().AsQueryable();
}

Now.. if I would change the repository method to return a List and also add .ToList for my result.. then this would work great. But then no matter what OData query comes in.. I would still first execute a "get all query".. turn them into a list and then I would execute my OData query against that list..

Which seems just plain wrong.. what I would like to do is to make sure that the OData query gets executed the same time as I'm trying to fetch data from the database.. so I only get the very specific result matching the query.. and not a huge bunch of data that then later on gets queried..

For now I have the problem with the DbContext getting disposed once outside the using.. but I still need to close the DbContext as well some where, some how..

Any ideas?


Solution

  • The latest version of the queryable extension of web api is kind of confusing, since it is so different from previous versions. In the new version you need to either explicitly enable queryable support or use the new query options class. See this

    Edit:Some code this since I am now at a desk

    public IQueryable<TModel> GetAll(ODataQueryOptions opts)
        {
            var db = _repo.GetAll();
            return (IQueryable<TModel>) opts.ApplyTo(db);
        }