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. mysql-5.6.34 Installation from Source code

    Took me a while to suffer from the first successful souce code installation of mysql-5.6.34. Just pu ...

  2. could not initialize proxy - no Session

    这是一个精典的问题:因为我们在hibernate里面load一个对象出来时,用到的是代理对象,也就是说当我们在执行load方法时并没有发sql语句,而是返回一个proxy对象.只有当们具体用到哪个ge ...

  3. Xamarin体验:使用C#开发iOS/Android应用

    Xamarin是Mono创始人Miguel de Icaza创建的公司,旨在让开发者可以用C#编写iOS, Android, Mac应用程序,也就是跨平台移动开发.   简介 Xamarin是基于Mo ...

  4. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...

  5. .NET基础拾遗(7)Web Service的开发与应用基础

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

  6. 企业IT管理员IE11升级指南【15】—— 代理自动配置脚本

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  7. windows10简单试用(多图,连薛定谔的猫都杀死了)

    为了大家看起来方便,我的截图都是gif的,比较小,但是颜色会有色差,相信大家不介意的 昨天windows10可以下载第一时间就下了玩玩 由于是技术预览,所以不打算替换之前的系统,只装在虚拟机里玩玩就好 ...

  8. JDBC基础

    今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...

  9. 前端学HTTP之缓存

    前面的话 Web缓存是可以自动保存常见文档副本的HTTP设备.当Web请求抵达缓存时,如果本地有“已缓存的”副本,就可以从本地存储设备而不是原始服务器中提取这个文档.本文将详细介绍缓存的相关内容 功能 ...

  10. Android混合开发之WebView使用总结

    前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...