jwanga
jwanga

Reputation: 4256

Translate odata uri to expression

I'd like to use an action filter to translate an Odata uri to a Linq expression. I'm doing this because i'm using the resulting expression to query nonSQL line of business systems. In the WCF web api this was trivial because the translated query was appended as a property of the the request object, as such:

var query = (EnumerableQuery)request.Properties["queryToCompose"];

That seems to have disappeared. Are there any public api's i can use to accomplish this?

Upvotes: 1

Views: 2828

Answers (3)

Peter Goodman
Peter Goodman

Reputation: 404

Check out Linq2Rest. It solves this problem.

Upvotes: -1

jwanga
jwanga

Reputation: 4256

So as it turns out the query has changed keys in the request property collection. It also seems that the internal filter that parses the query runs after the custom filters and thus doesn't add the query value. To get the translated query, call the following inside the controller action.

(EnumerableQuery<T>)this.Request.Properties["MS_QueryKey"];

Upvotes: 1

TheRightChoyce
TheRightChoyce

Reputation: 3084

I've been trying something similiar.. While not perfect, you can grab the OData expressions directly from the query string and build the LINQ expression manually:

var queryParams = HttpUtility.ParseQueryString( ControllerContext.Request.RequestUri.Query );
var top = queryParams.Get( "$top" );
var skip = queryParams.Get( "$skip" );
var orderby = queryParams.Get( "$orderby" );

And the apply that directly to your IQueryable or whatever you're using for the filtering. Not nearly as useful, but its a start.

Upvotes: 1

Related Questions