title: The best way to modify a WebAPI OData QueryOptions.Filter
body: <p>I am using the OData sample project at <a href="http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/working-with-entity-relations">http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/working-with-entity-relations</a>. In the Get I want to be able to change the Filter in the QueryOptions of the EntitySetController:</p>
  
  <pre><code>public class ProductsController : EntitySetController&lt;Product, int&gt;
  {
      ProductsContext _context = new ProductsContext();
  
      [Queryable(AllowedQueryOptions=AllowedQueryOptions.All)]
      public override IQueryable&lt;Product&gt; Get()
      {
          var products = QueryOptions.ApplyTo(_context.Products).Cast&lt;Product&gt;();
          return products.AsQueryable();
      }
  </code></pre>
  
  <p>I would like to be able to find properties that are specifically referred to. I can do this by parsing <code>this.QueryOptions.Filter.RawValue</code> for the property names but I cannot update the <code>RawValue</code> as it is read only. I can however create another instance of <code>FilterQueryOption</code> from the modified <code>RawValue</code> but I cannot assign it to <code>this.QueryOptions.Filter</code> as this is read only too.</p>
  
  <p>I guess I could call the new filter's <code>ApplyTo</code> passing it <code>_context.Products</code>, but then I will need to separately call the <code>ApplyTo</code> of the other properties of <code>QueryOptions</code> like <code>Skip</code> and <code>OrderBy</code>. Is there a better solution than this?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I tried the following:</p>
  
  <pre><code>    public override IQueryable&lt;Product&gt; Get()
      {
          IQueryable&lt;Product&gt; encryptedProducts = _context.Products;
  
          var filter = QueryOptions.Filter;
          if (filter != null &amp;&amp; filter.RawValue.Contains("Name"))
          {
              var settings = new ODataQuerySettings();
              var originalFilter = filter.RawValue;
              var newFilter = ParseAndEncyptValue(originalFilter);
              filter = new FilterQueryOption(newFilter, QueryOptions.Context);
              encryptedProducts = filter.ApplyTo(encryptedProducts, settings).Cast&lt;Product&gt;();
  
              if (QueryOptions.OrderBy != null)
              {
                  QueryOptions.OrderBy.ApplyTo&lt;Product&gt;(encryptedProducts);
              }
          }
          else
          {
              encryptedProducts = QueryOptions.ApplyTo(encryptedProducts).Cast&lt;Product&gt;();
          }
  
          var unencryptedProducts = encryptedProducts.Decrypt().ToList();
  
          return unencryptedProducts.AsQueryable();
      }
  </code></pre>
  
  <p>and it seems to be working up to a point. If I set a breakpoint I can see my products in the <code>unencryptedProducts</code> list, but when the method returns I don't get any items. I tried putting the <code>[Queryable(AllowedQueryOptions=AllowedQueryOptions.All)]</code> back on again but it had no effect. Any ideas why I am not getting an items?</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>I discovered that my query was being applied twice even though I am not using the <code>Queryable</code> attribute. This meant that even though I had items to return the List was being queried with the unencrypted value and therefore no values were being returned.</p>
  
  <p>I tried using an <code>ODataController</code> instead:</p>
  
  <pre><code>public class ODriversController : ODataController
  {
  
      //[Authorize()]
      //[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
      public IQueryable&lt;Products&gt; Get(ODataQueryOptions options)
      {
  </code></pre>
  
  <p>and this worked! Does this indicate that there is a bug in <code>EntitySetController</code>?</p>