修改web api 2 odata获取查询结果

时间:2016-08-26 18:18:30

标签: entity-framework odata webapp2

我有一个像bellow一样的Odata控制器get方法:

 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;
    }  
}

现在我想在将其发送到clinet之前修改我的查询结果...我希望像_list.Tolist()之类的东西然后遍历结果数组

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

我已经阅读了一些关于ActionFilterAttribute和ActionFilterAttribute.OnActionExecuted的内容。     HttpActionExecutedContext类,但我不知道如何实现我的想法

1 个答案:

答案 0 :(得分:0)

似乎您已经有一个关于EnableQuery Attribute的实现:public virtual IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions) ,您应该覆盖该方法:

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

首先获取查询结果,然后过滤结果:

$(document).ready(function(){
    $("#fmob").click(function(){
        var mobname = $(this).attr("data-value");
        console.log(mobname);
        var data = {}; // setup a data object
        data[mobname] = 1; // add the property with the key of the value of the "mobname" variable, with the data of 1 (per question)
        $.ajax({
            type: "POST",
            url: "/system/mobproc.php",
            data: data, // pass the data object as the POST data instead of defining an object inline
            dataType: "json",
            success: function(data){
                if(data.response === true){
                    $("#fresponse").html(data.result);  
                }else{
                    $("#fresponse").html(data.result);
                }
            },
            error:function(jqXHR,textStatus,errorThrown ){
              alert('Exception:'+errorThrown );
           }
        });
    });
});