Possible Duplicate:
OData pagination with WebApi ( $inlinecount )
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).
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);
}
}
Is there a way to get the a count separately, may be from the underlying provider of the IQueryable ?
Try this approach: http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-package/
It uses the latest Web API OData package.