How can I replace the $filter of a DataServiceQuery?

347 Views Asked by At

Lets say I have a query like this :

DataServiceQuery<Order> selectedOrders = context.Orders
    .AddQueryOption("$filter", "Freight gt 30")
    .AddQueryOption("$orderby", "OrderID desc");

I would like to append something like this using an IQueryable<Order>

.Where(o => o.OrderID > 10)

Is there a way this can be accomplished easily?

In my actual real-world scenario, both the existing Select/Expands and the .Where that is getting appended can be rather complex and I do not wish to attempt rewriting them with .AddQueryOption or Expressions. There are also a lot of them (it's for all the reports in our system), and the actual filter being added using AddQueryOption will be using a dynamic property name although the logic behind it should be the same.

I found I can cast the IQueryable to a DataServiceQuery as well and extract the $filter portion, but don't know how to alter/append it.

IQueryable<Order> orders = context.Orders
    .Where(o => o.OrderID > 10);

var dataServiceQuery = (DataServiceQuery<Order>)orders;
var filter = System.Web.HttpUtility.ParseQueryString(dataServiceQuery.RequestUri.Query)["$filter"];

// How do I change the filter to append " and Freight gt 30"?

I feel this should be something easy, but I'm still fairly to working with DataServiceQuery and my google skills are failing me.

0

There are 0 best solutions below