2

我正在使用 Netflix odata 服务来更好地了解如何使用 odata 数据。

在 VS 2010 中,我添加了对 NetFlix odata 服务的服务引用。然后我编写了这段代码,它只返回一些数据。

        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 并在浏览器中运行它。它返回的原子提要最后有这个元素

  <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'" />

这是一个链接,它将检索下一组数据(由 Netflix 完成的分页)。我的问题是如何让我的代码访问下一批数据和下一批等?

4

1 回答 1

7

查询可以转换为 DataServiceQuery,它有一个名为 Execute 的方法,它以 QueryOperationResponse 的形式返回结果,它有一个 GetContinuation 方法,它返回一个表示下一个链接的延续对象。遍历所有标题的粗略代码可能如下所示:

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);
}
于 2010-11-08T14:56:52.647 回答