方法: ページングされた結果を読み込む (WCF Data Services)
WCF Data Services では、1 つの応答フィードで返されるエンティティの数をデータ サービスで制限できます。 この処理を行うと、フィードの最終的なエントリには、データの次のページへのリンクが含まれます。 データの次のページの URI は、DataServiceQuery<TElement> が実行されたときに返される QueryOperationResponse<T> の GetContinuation メソッドを呼び出すことによって取得されます。このオブジェクトによって表される URI は、結果の次のページを読み込むために使用されます。 詳細については、「遅延コンテンツの読み込み (WCF Data Services)」を参照してください。
このトピックの例では、Northwind サンプル データ サービスおよび自動生成されたクライアント データ サービス クラスを使用します。 このサービスおよびクライアント データ クラスは、WCF Data Services クイック スタートを完了したときに作成されます。
使用例
次の例は、do…while ループを使用して、データ サービスからのページングされた結果の Customers エンティティを読み込みます。
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Dim token As DataServiceQueryContinuation(Of Customer) = Nothing
Dim pageCount = 0
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("Page {0}:", pageCount + 1)
' 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(vbTab & "Customer 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
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
DataServiceQueryContinuation<Customer> token = null;
int pageCount = 0;
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);
}
次の例は、関連する Orders エンティティと各 Customers エンティティを返し、do…while ループを使用して Customers エンティティ ページおよび入れ子になった while ループを読み込んで、データ サービスから関連する Orders エンティティのページを読み込みます。
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Dim nextLink As DataServiceQueryContinuation(Of Customer) = Nothing
Dim pageCount = 0
Dim innerPageCount = 0
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(vbTab & "Customer Name: {0}", c.CompanyName)
Console.WriteLine(vbTab & "Orders Page {0}:", innerPageCount + 1)
' 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(vbTab & vbTab & "OrderID: {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
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
DataServiceQueryContinuation<Customer> nextLink = null;
int pageCount = 0;
int innerPageCount = 0;
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 Data Services)