Search code examples
entity-frameworkodatawebapp2

Modifying web api 2 odata get query result


I have an Odata controller get method like bellow:

 public class ProductController : ApiController
{
    [MyEnableQuery(PageSize = 48, AllowedQueryOptions = AllowedQueryOptions.OrderBy | AllowedQueryOptions.Top | AllowedQueryOptions.Skip | AllowedQueryOptions.InlineCount | AllowedQueryOptions.Filter, AllowedFunctions = AllowedFunctions.SubstringOf | AllowedFunctions.ToLower)]

    public IQueryable<tbDefine_Products> GetProducts([FromODataUri] int CategoryID)
    {
        ProductHandler _handler = new ProductHandler();
        IQueryable<tbDefine_Products> _list =_handler.GetProductActiveList(CategoryID);
        return _list;
    }  
}

Now i wants to modify my query result before sending it to clinet... i want to something like _list.Tolist() and then iterating through the result array

       List<tbDefine_Products> _list2 = _list.ToList<tbDefine_Products>();
       for (int i = 0; i < _list2.Count; i++)
       {
        / *some code here to modify result */
       }

I have read a little about ActionFilterAttribute and ActionFilterAttribute.OnActionExecuted and HttpActionExecutedContext Classes but i dont know how implement my ideas


Solution

  • Seems that you have already have a implementation about EnableQuery Attribute : MyEnableQuery, you should override the method:

    public virtual IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
    

    get the query result first and then filter the result:

    var result = base.ApplyQuery(queryable, queryOptions);
    // filter the result.
    return result;