如何:加载分页结果(WCF 数据服务)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 如何:加载分页结果(WCF 数据服务)

如何:加载分页结果(WCF 数据服务)

 2013/10/29 14:57:53  Crazy大象  博客园  我要评论(0)
  • 摘要:WCF数据服务允许数据服务限制单个响应源中返回的实体数。在此情况下,源中的最后一项包含指向下一页数据的链接。通过调用执行DataServiceQuery时返回的QueryOperationResponse的GetContinuation方法可以获取下一页数据的URI。然后,可以使用此对象所表示的URI加载下一页结果。有关更多信息,请参见加载延迟的内容(WCF数据服务)。本主题中的示例使用Northwind示例数据服务和自动生成的客户端数据服务类
  • 标签:结果 数据 WCF 服务

class="sentence">WCF 数据服务 允许数据服务限制单个响应源中返回的实体数。在此情况下,源中的最后一项包含指向下一页数据的链接。通过调用执行 DataServiceQuery 时返回的 QueryOperationResponse 的 GetContinuation 方法可以获取下一页数据的 URI。然后,可以使用此对象所表示的 URI 加载下一页结果。有关更多信息,请参见加载延迟的内容(WCF 数据服务)。

本主题中的示例使用 Northwind 示例数据服务和自动生成的客户端数据服务类。此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。

示例

下面的示例使用 do¡­while 循环从数据服务的分页结果中加载 Customers 实体。

VB

logs_code_hide('2592f603-d20a-4be7-9b0d-b55bae47a7a1',event)" src="/Upload/Images/2013102914/2B1B950FA3DF188F.gif" alt="" />
 1 ' Create the DataServiceContext using the service URI.
 2 Dim context = New NorthwindEntities(svcUri)
 3 Dim token As DataServiceQueryContinuation(Of Customer) = Nothing
 4 Dim pageCount = 0
 5 
 6 Try
 7     ' Execute the query for all customers and get the response object.
 8     Dim response As QueryOperationResponse(Of Customer) = _
 9         CType(context.Customers.Execute(), QueryOperationResponse(Of Customer))
10 
11     ' With a paged response from the service, use a do...while loop 
12     ' to enumerate the results before getting the next link.
13     Do
14         ' Write the page number.
15         Console.WriteLine("Page {0}:", pageCount + 1)
16 
17         ' If nextLink is not null, then there is a new page to load.
18         If token IsNot Nothing Then
19             ' Load the new page from the next link URI.
20             response = CType(context.Execute(Of Customer)(token),  _
21             QueryOperationResponse(Of Customer))
22         End If
23 
24         ' Enumerate the customers in the response.
25         For Each customer As Customer In response
26             Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName)
27         Next
28 
29         ' Get the next link, and continue while there is a next link.
30         token = response.GetContinuation()
31     Loop While token IsNot Nothing
32 Catch ex As DataServiceQueryException
33     Throw New ApplicationException( _
34             "An error occurred during query execution.", ex)
35 End Try
View Code

C#

 1 // Create the DataServiceContext using the service URI.
 2 NorthwindEntities context = new NorthwindEntities(svcUri);
 3 DataServiceQueryContinuation<Customer> token = null;
 4 int pageCount = 0; 
 5 
 6 try
 7 { 
 8     // Execute the query for all customers and get the response object.
 9     QueryOperationResponse<Customer> response =
10         context.Customers.Execute() as QueryOperationResponse<Customer>;
11 
12     // With a paged response from the service, use a do...while loop 
13     // to enumerate the results before getting the next link.
14     do
15     {
16         // Write the page number.
17         Console.WriteLine("Page {0}:", pageCount++);
18 
19         // If nextLink is not null, then there is a new page to load.
20         if (token != null)
21         {
22             // Load the new page from the next link URI.
23             response = context.Execute<Customer>(token)
24                 as QueryOperationResponse<Customer>;
25         }
26 
27         // Enumerate the customers in the response.
28         foreach (Customer customer in response)
29         {
30             Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
31         }
32     }
33 
34     // Get the next link, and continue while there is a next link.
35     while ((token = response.GetContinuation()) != null);
36 }
37 catch (DataServiceQueryException ex)
38 {
39     throw new ApplicationException(
40         "An error occurred during query execution.", ex);
41 }
View Code

下面的示例返回每个 Customers 实体的相关 Orders 实体,并使用 do¡­while 循环从数据服务中加载 Customers 实体页以及使用嵌套的 while 循环从数据服务中加载相关的 Orders 实体的页。

VB

 1 ' Create the DataServiceContext using the service URI.
 2 Dim context = New NorthwindEntities(svcUri)
 3 Dim nextLink As DataServiceQueryContinuation(Of Customer) = Nothing
 4 Dim pageCount = 0
 5 Dim innerPageCount = 0
 6 
 7 Try
 8     ' Execute the query for all customers and related orders,
 9     ' and get the response object.
10     Dim response = _
11     CType(context.Customers.AddQueryOption("$expand", "Orders") _
12             .Execute(), QueryOperationResponse(Of Customer))
13 
14     ' With a paged response from the service, use a do...while loop 
15     ' to enumerate the results before getting the next link.
16     Do
17         ' Write the page number.
18         Console.WriteLine("Customers Page {0}:", ++pageCount)
19 
20         ' If nextLink is not null, then there is a new page to load.
21         If nextLink IsNot Nothing Then
22             ' Load the new page from the next link URI.
23             response = CType(context.Execute(Of Customer)(nextLink),  _
24                     QueryOperationResponse(Of Customer))
25         End If
26 
27         ' Enumerate the customers in the response.
28         For Each c As Customer In response
29             Console.WriteLine("\tCustomer Name: {0}", c.CompanyName)
30             Console.WriteLine("\tOrders Page {0}:", innerPageCount + 1)
31 
32             ' Get the next link for the collection of related Orders.
33             Dim nextOrdersLink As DataServiceQueryContinuation(Of Order) = _
34             response.GetContinuation(c.Orders)
35 
36             While nextOrdersLink IsNot Nothing
37                 For Each o As Order In c.Orders
38                     ' Print out the orders.
39                     Console.WriteLine("\t\tOrderID: {0} - Freight: ${1}", _
40                             o.OrderID, o.Freight)
41                 Next
42                 ' Load the next page of Orders.
43                 Dim ordersResponse = _
44                 context.LoadProperty(c, "Orders", nextOrdersLink)
45                 nextOrdersLink = ordersResponse.GetContinuation()
46             End While
47         Next
48         ' Get the next link, and continue while there is a next link.
49         nextLink = response.GetContinuation()
50     Loop While nextLink IsNot Nothing
51 Catch ex As DataServiceQueryException
52     Throw New ApplicationException( _
53             "An error occurred during query execution.", ex)
54 End Try
View Code

C#

 1 // Create the DataServiceContext using the service URI.
 2 NorthwindEntities context = new NorthwindEntities(svcUri);
 3 DataServiceQueryContinuation<Customer> nextLink = null;
 4 int pageCount = 0;
 5 int innerPageCount = 0;
 6 
 7 try
 8 {
 9     // Execute the query for all customers and related orders,
10     // and get the response object.
11     var response =
12         context.Customers.AddQueryOption("$expand", "Orders")
13         .Execute() as QueryOperationResponse<Customer>;
14 
15     // With a paged response from the service, use a do...while loop 
16     // to enumerate the results before getting the next link.
17     do
18     {
19         // Write the page number.
20         Console.WriteLine("Customers Page {0}:", ++pageCount);
21 
22         // If nextLink is not null, then there is a new page to load.
23         if (nextLink != null)
24         {
25             // Load the new page from the next link URI.
26             response = context.Execute<Customer>(nextLink)
27                 as QueryOperationResponse<Customer>;
28         }
29 
30         // Enumerate the customers in the response.
31         foreach (Customer c in response)
32         {
33             Console.WriteLine("\tCustomer Name: {0}", c.CompanyName);
34             Console.WriteLine("\tOrders Page {0}:", ++innerPageCount);
35             // Get the next link for the collection of related Orders.
36             DataServiceQueryContinuation<Order> nextOrdersLink = 
37                 response.GetContinuation(c.Orders);
38 
39             while (nextOrdersLink != null)
40             {
41                 foreach (Order o in c.Orders)
42                 {
43                     // Print out the orders.
44                     Console.WriteLine("\t\tOrderID: {0} - Freight: ${1}",
45                         o.OrderID, o.Freight);
46                 }
47 
48                 // Load the next page of Orders.
49                 var ordersResponse = context.LoadProperty(c, "Orders", nextOrdersLink);
50                 nextOrdersLink = ordersResponse.GetContinuation();
51             }
52         }
53     }
54 
55     // Get the next link, and continue while there is a next link.
56     while ((nextLink = response.GetContinuation()) != null);
57 }
58 catch (DataServiceQueryException ex)
59 {
60     throw new ApplicationException(
61         "An error occurred during query execution.", ex);
62 }
View Code

 

本文来自:http://technet.microsoft.com/zh-cn

发表评论
用户名: 匿名