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. SVN版本冲突,导致出现Files 的值“ < < < < < < < .mine”无效

    只要根据错误提示,找到相应文件夹下的\obj\Debug文件夹下的 相应名字.csproj.FileListAbsolute.txt, 打开并删除含有'<<<<<< ...

  2. .NET基础拾遗(1)类型语法基础和内存管理基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...

  3. Windbg Extension NetExt 使用指南 【3】 ---- 挖掘你想要的数据 Managed Heap

    摘要 : NetExt中有两个比较常用的命令可以用来分析heap上面的对象. 一个是!wheap, 另外一个是!windex. !wheap 这个命令可以用于打印出heap structure信息. ...

  4. 分布式理论之一:Paxos算法的通俗理解

    维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的"La",此人现在在微软研究院)于1990年提出的一种基于消息传递且具有高度容错特性 ...

  5. 模拟实现Spring中的注解装配

    本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...

  6. Html5 希尔排序演示

    希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本. 如下图所示: 代码如下: <!DOCTYPE html> <html& ...

  7. CentOS 搭建openVPN

    1.安装前准备 # 关闭selinux setenforce 0 sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config # 安装open ...

  8. [转]VS2015中臃肿的ipch和sdf文件

    使用VS2010建立C++解决方案时,会生成SolutionName.sdf和一个叫做ipch的文件夹,这两个文件再加上*.pch等文件使得工程变得非常的庞大, 一个简单的程序都会占用几十M的硬盘容量 ...

  9. 设计模式之迪米特原则(LOD)(最少知识原则)

    来源:迪米特法则(LoD)最初是用来作为面向对象的系统设计风格的一种法则,是很多著名系统,如火星登陆软件系统.木星的欧罗巴卫星轨道飞船的软件系统的指导设计原则. 迪米特法则(LoD)又可分为两种:狭义 ...

  10. 三、Redis基本操作——List

    小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...