Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用过滤器i';将Linq语句添加到Web API Get;我正在尝试添加$select_Linq_Asp.net Web Api_Odata - Fatal编程技术网

使用过滤器i';将Linq语句添加到Web API Get;我正在尝试添加$select

使用过滤器i';将Linq语句添加到Web API Get;我正在尝试添加$select,linq,asp.net-web-api,odata,Linq,Asp.net Web Api,Odata,我正在尝试将一些linq语句应用于所有Get Web api命令。我想我可以使用actionfilteratAttribute来实现这一点 我基本上是在WebAPI中添加$select支持,因为它目前不受支持。我不确定在哪里可以获得IQueryable结果。我相信在sql执行之前需要它,但在Get函数返回IQueryable结果之后需要它。任何帮助都会很好。我正在尝试类似的方法,但他的想法行不通,因为httpresponsemessageresponse=actionexecutecontext

我正在尝试将一些linq语句应用于所有Get Web api命令。我想我可以使用
actionfilteratAttribute
来实现这一点

我基本上是在WebAPI中添加$select支持,因为它目前不受支持。我不确定在哪里可以获得
IQueryable
结果。我相信在sql执行之前需要它,但在Get函数返回
IQueryable
结果之后需要它。任何帮助都会很好。我正在尝试类似的方法,但他的想法行不通,因为
httpresponsemessageresponse=actionexecutecontext.Result不再在RC中

谢谢 尼克


解决方案
见上面的原始帖子。我在底部添加了解决方案。

我已经构建了一个解决方案,明天我将在清理代码后发布它。
        public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        HttpRequestMessage request = actionExecutedContext.Request;
        HttpResponseMessage response = actionExecutedContext.Response;

        IQueryable obj;

        if (response != null && response.TryGetContentValue(out obj) && request.RequestUri.ParseQueryString()["$select"] != null)
        {

            System.Collections.Specialized.NameValueCollection QueryItems = request.RequestUri.ParseQueryString();
            string select = QueryItems["$select"];

            if (!string.IsNullOrWhiteSpace(select))
            {
                obj = obj.Select(string.Format("new ({0})", select));

            }

            //
            //this should be generic not hard coded for Json
            //
            string json = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);

            actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse();
            actionExecutedContext.Response.Content = new StringContent(json);
            actionExecutedContext.Response.Content.Headers.Clear();
            actionExecutedContext.Response.Content.Headers.Add("Content-Type", "application/json");
            actionExecutedContext.Response.StatusCode = System.Net.HttpStatusCode.OK;

        }


    }