Search code examples
wcf-data-servicesodata

Filtering in OdataQueryOpeation


I am creating a custom Data Service Provider for some data structure which is in RavenDB.

I am using ODataContext from WCF Data Service Toolkit, and have some part of query working like TOP / SKIP etc.

public IEnumerable<Movie> GetAll(ODataQueryOperation operation)
{           
    IQueryable<Movie> query;
    if (operation.TopCount != 0)
        query = _session.Query<Movie>().Skip(operation.SkipCount).Take(operation.TopCount);
}

This code supports queries like :

http://<url>/oDataService.svc/Movies?$top=2

But I can not make filtering to work.

operation.FilterExpression has type-expression, and it has value:it=>(it.ReleaseYear == 2012). I tried code below, but value of 'expression' is coming null.

Expression<Func<Movie, bool>> expression = operation.FilterExpression as Expression<Func<Movie, bool>>;
query = _session.Query<Movie>().Where(expression);

And query fired from browser is below:

http:///oDataService.svc/Movies?$filter = ReleaseYear eq 2012


Solution

  • If you want to get to the actual Expression you could try the following

    var where = (operation.FilterExpression as UnaryExpression).Operand as Expression<Func<Movie, bool>>
    

    This will give a type of Expression<Func<Movie,bool>> that you can use directly with with the IQueryable.

    query = _session.Query<Movie>().Where(where);