paging through NetFlix odata results

大憨熊 提交于 2019-12-13 15:51:07

问题


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&amp;$orderby=AverageRating%20desc&amp;$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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!