使用asp.net的WebAPI实现的OData inlinecount [英] implementing oData inlinecount using asp.net webapi

查看:241
本文介绍了使用asp.net的WebAPI实现的OData inlinecount的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
   OData的分页与的WebAPI($ inlinecount)

由于Asp.net的WebAPI几乎支持ODATA,它非常诱人,我得到$ inlinecount工作,使之与剑道UI(或任何其他)起着很好。
因此,它JSONP格式返回值,我实现了一个新的MediaFormatter(从#1)。

Since Asp.net WebAPi almost supports odata, its very enticing for me to get $inlinecount to work so that it plays nicely with kendo ui ( or any other). So that it returns value in jsonp format, i implemented a new MediaFormatter ( from Stackoverflow).

麻烦的是,它需要的结果有计数元素在其中,以获得服务器端分页的工作,所以现在我砍死格式化得到一个假的计数工作。这一切的伟大工程和电网都是幸福的,但是得到真正的计数是自从被已经回到前的IQueryable pression挑战具有过滤器/送等适用于它。

Trouble is that it needs the results to have count element in them in order to get server side paging to work, so for now i hacked the formatter to get a fake count to work. This all works great and the grid is all happy, however getting the real count is a challenge since IQueryable expression being returned already has filters/Take etc applied to it.

 public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext transportContext)
    {
        string callback;
        if (IsJsonpCountableRequest(out callback))
        {
            return Task.Factory.StartNew(() =>
            {
                var q = value as IQueryable<Movie>;
                var count = q.Count(); // this count doesnt return the actual count
                var writer = new StreamWriter(stream);
                writer.Write(callback + "({");
                writer.Write(@"""d""");
                writer.Write(" : { ");
                writer.Write(@"""results""");
                writer.Write(" : ");
                writer.Flush();
                base.WriteToStreamAsync(type, value, stream, contentHeaders, transportContext).Wait();
                writer.Write(",");
                writer.Write(@"""__count""");
                writer.Write(" : ");

                writer.Write(string.Format(@"""{0}""", count));
                writer.Write("}");

                writer.Write("})");
                writer.Flush();
            });
        }
        else
        {
            return base.WriteToStreamAsync(type, value, stream, contentHeaders, transportContext);
        }
    }

有没有办法单独获得计数,可能是从IQueryable的?

Is there a way to get the a count separately, may be from the underlying provider of the IQueryable ?

推荐答案

试试这个办法:<一href=\"http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-$p$pview-package/\" rel=\"nofollow\">http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-$p$pview-package/

它采用了最新的Web API的OData包。

It uses the latest Web API OData package.

这篇关于使用asp.net的WebAPI实现的OData inlinecount的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆