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

如何:加载分页结果(WCF 数据服务)的更多相关文章

  1. android左右滑动加载分页以及动态加载数据

    android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: package cn.anycall.ju; import java.util.ArrayList; import java ...

  2. jqgrid 分页时,清空原表格数据加载返回的新数据

    由于,我们是动态分页,分页后的数据是在触发分页后动态加载而来.如何使jqgrid清空原数据而加载新数据? 1)调用jqgrid的 clearGridData 方法清空表格数据 2)调用jqgrid的  ...

  3. openlayer3 加载geoserver发布的WFS服务

    转自原文 openlayer3加载geoserver发布的WFS服务 openlayers3调用GeoServer发布的wfs 1 参考一 1.1 问题 openlayer3加载WFS存在跨域问题,需 ...

  4. Smart3D系列教程7之 《手动配置S3C索引加载全部的瓦片数据》

    一.前言 迄今为止,Wish3D已经出品推出了6篇系列教程,从倾斜摄影的原理方法.采集照片的技巧.Smart3D各模块的功能应用.小物件的照片重建.大区域的地形重建到DSM及正射影像的处理生产,立足于 ...

  5. KnockoutJS 3.X API 第七章 其他技术(1) 加载和保存JSON数据

    Knockout允许您实现复杂的客户端交互性,但几乎所有Web应用程序还需要与服务器交换数据,或至少将本地存储的数据序列化. 最方便的交换或存储数据的方式是JSON格式 - 大多数Ajax应用程序今天 ...

  6. WCF服务与WCF数据服务的区别

    问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data  Se ...

  7. WCF 数据服务 4.5

    .NET Framework 4.5 其他版本 WCF 数据服务(以前称为"ADO.NET Data Services")是 .NET Framework 的一个组件.可以使用此组 ...

  8. Knockout应用开发指南 第六章:加载或保存JSON数据

    原文:Knockout应用开发指南 第六章:加载或保存JSON数据 加载或保存JSON数据 Knockout可以实现很复杂的客户端交互,但是几乎所有的web应用程序都要和服务器端交换数据(至少为了本地 ...

  9. sql2008r2,以前好好可以用的,但装了vs2017后,连接不上了,服务也停了,结果手动也 启动不了, 无法加载或初始化请求的服务提供程

    日志: 2017-12-14 12:33:17.53 服务器 A self-generated certificate was successfully loaded for encryption.2 ...

随机推荐

  1. Android Socket连接PC出错问题及解决

    最近测试问题:Android 通过Socket链接电脑,ip和端口都是正确的,也在同一网段,可android端就是报异常如下: 解决办法:测试电脑的防火墙可能开着,在控制面板把防火墙打开即可.

  2. 给缺少Python项目实战经验的人

    我们在学习过程中最容易犯的一个错误就是:看的多动手的少,特别是对于一些项目的开发学习就更少了! 没有一个完整的项目开发过程,是不会对整个开发流程以及理论知识有牢固的认知的,对于怎样将所学的理论知识应用 ...

  3. Linux 入门之网络配置

    查看网络状态 ifconfig 修改网络参数 实验环境centos6.5,其他系统自行百度 ls /etc/sysconfig/network-scripts 显示所有文件, vi /etc/sysc ...

  4. Windows Server 2008 R2常规安全设置及基本安全策略

    这篇文章主要介绍了Windows Web Server 2008 R2服务器简单安全设置,需要的朋友可以参考下 用的腾讯云最早选购的时候悲催的只有Windows Server 2008 R2的系统,原 ...

  5. 多用多学之Java中的Set,List,Map

            很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...

  6. 学习笔记:7z在delphi的应用

    最近做个发邮件的功能,需要将日志文件通过邮件发送回来用于分析,但是日志文件可能会超级大,测算下来一天可能会有800M的大小.所以压缩是不可避免了,delphi中的默认压缩算法整了半天不太好使,就看了看 ...

  7. raspberrypi(树莓派)上安装mono和jexus,运行asp.net程序

    参考网址: http://www.linuxdot.net/ http://www.cnblogs.com/mayswind/p/3279380.html http://www.raspberrypi ...

  8. 【腾讯Bugly干货分享】聊聊苹果的Bug - iOS 10 nano_free Crash

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/hnwj24xqrtOhcjEt_TaQ9w 作者:张 ...

  9. WinRT自定义控件第一 - 转盘按钮控件

    之前的文章中,介绍了用WPF做一个转盘按钮控件,后来需要把这个控件移植到WinRT时,遇到了很大的问题,主要原因在于WPF和WinRT还是有很大不同的.这篇文章介绍了这个移植过程,由于2次实现的控件功 ...

  10. ucos实时操作系统学习笔记——任务间通信(信号量)

    ucos实时操作系统的任务间通信有好多种,本人主要学习了sem, mutex, queue, messagebox这四种.系统内核代码中,这几种任务间通信机制的实现机制相似,接下来记录一下本人对核心代 ...