通过NetFlix odata结果进行分页 [英] paging through NetFlix odata results
问题描述
我正在使用Netflix的odata服务,以更好地了解如何使用odata数据.
I am playing around with the Netflix odata service to get a better understanding of how to consume odata data.
在VS 2010中,我向NetFlix odata服务添加了服务引用.然后,我编写了此代码,该代码仅返回一些数据.
In VS 2010 I added a service reference to the NetFlix odata service. I then wrote this code which returns only some of the data.
var cat = new NetflixCatalog(new Uri("http://odata.netflix.com/v1/Catalog/"));
var x = from t in cat.Titles
where t.ReleaseYear == 2009
select t;
foreach (Title title in x)
{
ProcessTitle(title);
}
我查看了为调用生成的uri,并在浏览器中运行了它.它返回的原子feed的末尾有这个元素
I looked at the uri generated for the call and ran it in a browser. The atom feed it returns has this element at the end
<link rel="next" href="http://odata.netflix.com:20000/v1/Catalog/Titles()/?$filter=ReleaseYear%20eq%202009&$orderby=AverageRating%20desc&$skiptoken=3.9D,'BVqRa'" />
这是一个链接,它将检索下一组数据(由Netflix完成分页).我的问题是如何获取代码来访问下一批数据和下一批数据?
This is the a link that will retrieve the next set of data (paging done by Netflix). My question is how do I get my code to access this next batch of data and the next etc.?
推荐答案
查询可以强制转换为DataServiceQuery,该方法具有一个名为Execute的方法,该方法以QueryOperationResponse的形式返回结果,该方法具有一个GetContinuation方法,该方法返回一个表示对象的延续对象.下一个链接. 遍历所有标题的粗略代码如下所示:
The query can be cast to DataServiceQuery, which has a method called Execute which returns the results as QueryOperationResponse which has a GetContinuation method, which returns a continuation object representing the next link. A rough code to go through all the titles could look like this:
var cat = new NetflixCatalog(new Uri("http://odata.netflix.com/v1/Catalog/"));
var x = from t in cat.Titles
where t.ReleaseYear == 2009
select t;
var response = (QueryOperationResponse<Title>)((DataServiceQuery<Title>)x).Execute();
while (true)
{
foreach (Title title in response)
{
Console.WriteLine(title.Name);
}
var continuation = response.GetContinuation();
if (continuation == null)
{
break;
}
response = cat.Execute(continuation);
}
这篇关于通过NetFlix odata结果进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!