WCF 数据服务 允许数据服务限制单个响应源中返回的实体数。在此情况下,源中的最后一项包含指向下一页数据的链接。通过调用执行 DataServiceQuery 时返回的 QueryOperationResponseGetContinuation 方法可以获取下一页数据的 URI。然后,可以使用此对象所表示的 URI 加载下一页结果。有关更多信息,请参见加载延迟的内容(WCF 数据服务)

本主题中的示例使用 Northwind 示例数据服务和自动生成的客户端数据服务类。此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。

示例

下面的示例使用 do¡­while 循环从数据服务的分页结果中加载 Customers 实体。

VB

 ' Create the DataServiceContext using the service URI.
 Dim context = New NorthwindEntities(svcUri)
 Dim token As DataServiceQueryContinuation(Of Customer) = Nothing

 Try
     ' Execute the query for all customers and get the response object.
     Dim response As QueryOperationResponse(Of Customer) = _
         CType(context.Customers.Execute(), QueryOperationResponse(Of Customer))

     ' With a paged response from the service, use a do...while loop
     ' to enumerate the results before getting the next link.
     Do
         ' Write the page number.
         Console.WriteLine()

         ' If nextLink is not null, then there is a new page to load.
         If token IsNot Nothing Then
             ' Load the new page from the next link URI.
             response = CType(context.Execute(Of Customer)(token),  _
             QueryOperationResponse(Of Customer))
         End If

         ' Enumerate the customers in the response.
         For Each customer As Customer In response
             Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName)
         Next

         ' Get the next link, and continue while there is a next link.
         token = response.GetContinuation()
     Loop While token IsNot Nothing
 Catch ex As DataServiceQueryException
     Throw New ApplicationException( _
             "An error occurred during query execution.", ex)
 End Try

C#

 // Create the DataServiceContext using the service URI.
 NorthwindEntities context = new NorthwindEntities(svcUri);
 DataServiceQueryContinuation<Customer> token = null;
 ; 

 try
 {
     // Execute the query for all customers and get the response object.
     QueryOperationResponse<Customer> response =
         context.Customers.Execute() as QueryOperationResponse<Customer>;

     // With a paged response from the service, use a do...while loop
     // to enumerate the results before getting the next link.
     do
     {
         // Write the page number.
         Console.WriteLine("Page {0}:", pageCount++);

         // If nextLink is not null, then there is a new page to load.
         if (token != null)
         {
             // Load the new page from the next link URI.
             response = context.Execute<Customer>(token)
                 as QueryOperationResponse<Customer>;
         }

         // Enumerate the customers in the response.
         foreach (Customer customer in response)
         {
             Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
         }
     }

     // Get the next link, and continue while there is a next link.
     while ((token = response.GetContinuation()) != null);
 }
 catch (DataServiceQueryException ex)
 {
     throw new ApplicationException(
         "An error occurred during query execution.", ex);
 }

下面的示例返回每个 Customers 实体的相关 Orders 实体,并使用 do¡­while 循环从数据服务中加载 Customers 实体页以及使用嵌套的 while 循环从数据服务中加载相关的 Orders 实体的页。

VB

 ' Create the DataServiceContext using the service URI.
 Dim context = New NorthwindEntities(svcUri)
 Dim nextLink As DataServiceQueryContinuation(Of Customer) = Nothing

 Try
     ' Execute the query for all customers and related orders,
     ' and get the response object.
     Dim response = _
     CType(context.Customers.AddQueryOption("$expand", "Orders") _
             .Execute(), QueryOperationResponse(Of Customer))

     ' With a paged response from the service, use a do...while loop
     ' to enumerate the results before getting the next link.
     Do
         ' Write the page number.
         Console.WriteLine("Customers Page {0}:", ++pageCount)

         ' If nextLink is not null, then there is a new page to load.
         If nextLink IsNot Nothing Then
             ' Load the new page from the next link URI.
             response = CType(context.Execute(Of Customer)(nextLink),  _
                     QueryOperationResponse(Of Customer))
         End If

         ' Enumerate the customers in the response.
         For Each c As Customer In response
             Console.WriteLine("\tCustomer Name: {0}", c.CompanyName)
             Console.WriteLine()

             ' Get the next link for the collection of related Orders.
             Dim nextOrdersLink As DataServiceQueryContinuation(Of Order) = _
             response.GetContinuation(c.Orders)

             While nextOrdersLink IsNot Nothing
                 For Each o As Order In c.Orders
                     ' Print out the orders.
                     Console.WriteLine("\t\tOrderID: {0} - Freight: ${1}", _
                             o.OrderID, o.Freight)
                 Next
                 ' Load the next page of Orders.
                 Dim ordersResponse = _
                 context.LoadProperty(c, "Orders", nextOrdersLink)
                 nextOrdersLink = ordersResponse.GetContinuation()
             End While
         Next
         ' Get the next link, and continue while there is a next link.
         nextLink = response.GetContinuation()
     Loop While nextLink IsNot Nothing
 Catch ex As DataServiceQueryException
     Throw New ApplicationException( _
             "An error occurred during query execution.", ex)
 End Try

C#

 // Create the DataServiceContext using the service URI.
 NorthwindEntities context = new NorthwindEntities(svcUri);
 DataServiceQueryContinuation<Customer> nextLink = null;
 ;
 ;

 try
 {
     // Execute the query for all customers and related orders,
     // and get the response object.
     var response =
         context.Customers.AddQueryOption("$expand", "Orders")
         .Execute() as QueryOperationResponse<Customer>;

     // With a paged response from the service, use a do...while loop
     // to enumerate the results before getting the next link.
     do
     {
         // Write the page number.
         Console.WriteLine("Customers Page {0}:", ++pageCount);

         // If nextLink is not null, then there is a new page to load.
         if (nextLink != null)
         {
             // Load the new page from the next link URI.
             response = context.Execute<Customer>(nextLink)
                 as QueryOperationResponse<Customer>;
         }

         // Enumerate the customers in the response.
         foreach (Customer c in response)
         {
             Console.WriteLine("\tCustomer Name: {0}", c.CompanyName);
             Console.WriteLine("\tOrders Page {0}:", ++innerPageCount);
             // Get the next link for the collection of related Orders.
             DataServiceQueryContinuation<Order> nextOrdersLink =
                 response.GetContinuation(c.Orders);

             while (nextOrdersLink != null)
             {
                 foreach (Order o in c.Orders)
                 {
                     // Print out the orders.
                     Console.WriteLine("\t\tOrderID: {0} - Freight: ${1}",
                         o.OrderID, o.Freight);
                 }

                 // Load the next page of Orders.
                 var ordersResponse = context.LoadProperty(c, "Orders", nextOrdersLink);
                 nextOrdersLink = ordersResponse.GetContinuation();
             }
         }
     }

     // Get the next link, and continue while there is a next link.
     while ((nextLink = response.GetContinuation()) != null);
 }
 catch (DataServiceQueryException ex)
 {
     throw new ApplicationException(
         "An error occurred during query execution.", ex);
 }

本文来自:http://technet.microsoft.com/zh-cn

最新文章

  1. unigui MessageDlg方法调用例子
  2. IIS启用.net2.0
  3. 基于WORDPRESS+MYSQL的绿色企业主题制作全过程
  4. ASP.NET MVC 学习1、新增Controller,了解MVC运行机制
  5. bzoj1976
  6. WPF DataGrid 增加&quot;更新&quot;模板列,根据行Row的选择而显示&quot;更新&quot;按钮
  7. windows phone (15) UI变换上
  8. 循序渐进看Java web日志跟踪(1)-Tomcat 日志追踪与配置
  9. 【HTML】dl dt dd
  10. java程序设计 彩票购买抽奖程序 团队博客
  11. JavaScript设计模式 Item 2 -- 接口的实现
  12. flask请求异步执行(转载)
  13. thinkphp如何利用反射实现钩子方法
  14. 在 Ubuntu13.10 服务器中安装 Munin(监视工具)【转】
  15. Assert随笔
  16. 【Python】两个for循环嵌套练习
  17. 利用MacBookPro入侵无线网络
  18. 简单配置webpack4 + vue
  19. Eclipse插件开发 学习笔记 PDF 第一篇到第四篇 免分下载 开发基础 核心技术 高级进阶 综合实例
  20. JAVA 1.5 并发之 Executor框架 (二)execute VS submit

热门文章

  1. iOS 10 跳转系统设置
  2. ionic第一坑——ion-slide-box坑(ion-slide分两页的坑)
  3. Oracle 列数据聚合方法汇总
  4. Linux C语言解析并显示.bmp格式图片
  5. 《高性能javascript》一书要点和延伸(上)
  6. .NET基础拾遗(1)类型语法基础和内存管理基础
  7. RabbitMQ基础知识
  8. CSharpGL(37)创建和使用VBO的最佳方式
  9. 迟来的Json反序列化
  10. ABP(现代ASP.NET样板开发框架)系列之10、ABP领域层——实体