问题
I am playing around with the Netflix odata service to get a better understanding of how to consume odata data.
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);
}
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'" />
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.?
回答1:
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);
}
来源:https://stackoverflow.com/questions/4120467/paging-through-netflix-odata-results