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. win10上部署Hadoop-2.7.3——非Cygwin、非虚拟机

    开始接触Hadoop,听人说一般都是在Lunix下部署Hadoop,但是本人Lunix不是很了解,所以Google以下如何在Win10下安装Hadoop(之后再在Lunix下弄),找到不少文章,以下是 ...

  2. Android Weekly Notes Issue #237

    Android Weekly Issue #237 December 25th, 2016 Android Weekly Issue #237 这是本年的最后一篇issue, 感谢大家. 本期内容包括 ...

  3. Atitit.cto 与技术总监的区别

    Atitit.cto 与技术总监的区别 1. 核心区别1 2. Cto主要职责1 3. 如何提升到cto1 4. CTO五种基本的必备素质:2 5. 2 1. 核心区别 技术总监(Chief Tech ...

  4. VMware下对虚拟机Ubuntu14系统所在分区sda1进行磁盘扩容

    VMware下对虚拟机Ubuntu14系统所在分区sda1进行磁盘扩容 一般来说,在对虚拟机里的Ubuntu下的磁盘进行扩容时,都是添加新的分区,而并不是对其系统所在分区进行扩容,如在此链接中http ...

  5. NodeJs 开发微信公众号(四)微信网页授权

    微信的网页授权指的是在微信公众号中访问第三方网页时获取用户地理.个人等信息的权限.对于开发了自己的网页app应用时,获取个人的信息非常重要.上篇博客讲到了注册时可以获取用户的信息,很多人会问为什么还需 ...

  6. Kinect开发文章目录

    整理了一下去年为止到现在写的和翻译的Kinect的相关文章,方便大家查看.另外,最近京东上微软在搞活动, 微软 Kinect for Windows 京东十周年专供礼包 ,如果您想从事Kinect开发 ...

  7. 那些年黑了你的微软BUG

    本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 前言 炎炎夏日,朗朗乾坤,30℃ 的北京,你还在 Coding 吗? 整个 7 月都在忙项目,还加了 ...

  8. [异常解决] JLINK 与STM32的SWD连接接线方式

    如果我们的板子上只留了4个接口:V3.3,SWDIO,SWDCLK,GND.那么和JTAG的连接关系参见下图: 注意缺口方向.然后GND可以接左边任何一个pin(除了最底下这个PIN). FROM: ...

  9. out和ref详解

    要想充分理解C# out和ref,必须先明确如下两个概念(对值类型与引用类型掌握比较好的,可以跳过"一.明确两个基本概念") 一.明确两个基本概念 值类型: 定义:通过值的方式来传 ...

  10. java运行时获得泛型类型

    引言 众所周知,java泛型最重要的特征是泛型擦除,所有泛型在编译时会转换成Object所以在java中运行时无法获得泛型的类型. 但是其实以上的规则是针对方法的内部变量的,如果是其他形式的泛型其实是 ...