如何:加载分页结果(WCF 数据服务)
WCF 数据服务 允许数据服务限制单个响应源中返回的实体数。在此情况下,源中的最后一项包含指向下一页数据的链接。通过调用执行 DataServiceQuery 时返回的 QueryOperationResponse 的 GetContinuation 方法可以获取下一页数据的 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); }
如何:加载分页结果(WCF 数据服务)的更多相关文章
- android左右滑动加载分页以及动态加载数据
android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: package cn.anycall.ju; import java.util.ArrayList; import java ...
- jqgrid 分页时,清空原表格数据加载返回的新数据
由于,我们是动态分页,分页后的数据是在触发分页后动态加载而来.如何使jqgrid清空原数据而加载新数据? 1)调用jqgrid的 clearGridData 方法清空表格数据 2)调用jqgrid的 ...
- openlayer3 加载geoserver发布的WFS服务
转自原文 openlayer3加载geoserver发布的WFS服务 openlayers3调用GeoServer发布的wfs 1 参考一 1.1 问题 openlayer3加载WFS存在跨域问题,需 ...
- Smart3D系列教程7之 《手动配置S3C索引加载全部的瓦片数据》
一.前言 迄今为止,Wish3D已经出品推出了6篇系列教程,从倾斜摄影的原理方法.采集照片的技巧.Smart3D各模块的功能应用.小物件的照片重建.大区域的地形重建到DSM及正射影像的处理生产,立足于 ...
- KnockoutJS 3.X API 第七章 其他技术(1) 加载和保存JSON数据
Knockout允许您实现复杂的客户端交互性,但几乎所有Web应用程序还需要与服务器交换数据,或至少将本地存储的数据序列化. 最方便的交换或存储数据的方式是JSON格式 - 大多数Ajax应用程序今天 ...
- WCF服务与WCF数据服务的区别
问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data Se ...
- WCF 数据服务 4.5
.NET Framework 4.5 其他版本 WCF 数据服务(以前称为"ADO.NET Data Services")是 .NET Framework 的一个组件.可以使用此组 ...
- Knockout应用开发指南 第六章:加载或保存JSON数据
原文:Knockout应用开发指南 第六章:加载或保存JSON数据 加载或保存JSON数据 Knockout可以实现很复杂的客户端交互,但是几乎所有的web应用程序都要和服务器端交换数据(至少为了本地 ...
- sql2008r2,以前好好可以用的,但装了vs2017后,连接不上了,服务也停了,结果手动也 启动不了, 无法加载或初始化请求的服务提供程
日志: 2017-12-14 12:33:17.53 服务器 A self-generated certificate was successfully loaded for encryption.2 ...
随机推荐
- Android Weekly Notes Issue #235
Android Weekly Issue #235 December 11th, 2016 Android Weekly Issue #235 本期内容包括: 开发一个自定义View并发布为开源库的完 ...
- Linux下用netstat查看网络状态、端口状态(转)
转:http://blog.csdn.net/guodongdongnumber1/article/details/11383019 在linux一般使用netstat 来查看系统端口使用情况步. ...
- 换个角度看微信小程序[推荐]
去年参加几次技术沙龙时,我注意到一个有意思的现象:与之前大家统一接受的换名片不同,有些人并不愿意被添加微信好友--"不好意思,不熟的人不加微信". 这个现象之所以有意思,是因为名片 ...
- [Top-Down Approach]Take Notes
Computer Networking - A Top-Down Approach Six Edition Learn HTTP Using Browser and Proxy 2016-03-20 ...
- 虚拟机体验之 Xen 篇 —— 令人脑洞大开的奇异架构
这一篇我要体验的虚拟机系统是 Xen.在虚拟机领域,Xen 具有非常高的知名度,其名字经常在各类文章中出现.同时 Xen 也具有非常高的难度,别说玩转,就算仅仅只是理解它,都不是那么容易.之所以如此, ...
- ABP源码分析三十四:ABP.Web.Mvc
ABP.Web.Mvc模块主要完成两个任务: 第一,通过自定义的AbpController抽象基类封装ABP核心模块中的功能,以便利的方式提供给我们创建controller使用. 第二,一些常见的基础 ...
- node.js学习总结(一)
1.1.1 安装 Node.js 有三种方式安装 Node.js:一是通过安装包安装,二是通过源码编译安装,三是在 Linux 下可以通过 yum|apt-get 安装,在 Mac 下可以通过 Hom ...
- JavaScript资源大全中文版(Awesome最新版)
Awesome系列的JavaScript资源整理.awesome-javascript是sorrycc发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架.运行器.QA.MVC框架和库.模 ...
- 从零开始编写自己的C#框架(23)——上传组件使用说明
文章导航 1.前言 2.上传组件功能说明 3.数据库结构 4.上传配置管理 5.上传组件所使用到的类 6.上传组件调用方法 7.效果演示 8.小结 1.前言 本系列所使用的是上传组件是大神July开发 ...
- web前端性能调优
最近2个月一直在做手机端和电视端开发,开发的过程遇到过各种坑.弄到快元旦了,终于把上线了.2个月干下来满满的的辛苦,没有那么忙了自己准备把前端的性能调优总结以下,以方便以后自己再次使用到的时候得于得心 ...