方法: クエリで返されたエンティティの数を確認する (WCF Data Services)

WCF Data Services を使用すると、クエリ URI によって指定されたエンティティ セット内のエンティティの数を確認できます。 この数は、クエリ結果と一緒に、または整数値として含まれます。 詳細については、「データ サービスのクエリ (WCF Data Services)」を参照してください。

このトピックの例では、Northwind サンプル データ サービスおよび自動生成されたクライアント データ サービス クラスを使用します。 このサービスおよびクライアント データ クラスは、WCF Data Services クイック スタートを完了したときに作成されます。

次の例では、IncludeTotalCount メソッドを呼び出した後でクエリを実行します。 TotalCount プロパティが、Customers エンティティ セット内のエンティティの数を返します。

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define a new query for Customers that includes the total count.
Dim query As DataServiceQuery(Of Customer) = _
context.Customers.IncludeTotalCount()

Try
    ' Execute the query for all customers and get the response object.
    Dim response As QueryOperationResponse(Of Customer) = _
        CType(query.Execute(), QueryOperationResponse(Of Customer))

    ' Retrieve the total count from the response.
    Console.WriteLine("There are a total of {0} customers.", response.TotalCount)

    ' Enumerate the customers in the response.
    For Each customer As Customer In response
        Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName)
    Next
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);
         
// Define a new query for Customers that includes the total count.
DataServiceQuery<Customer> query = context.Customers.IncludeTotalCount();

try
{    
    // Execute the query for all customers and get the response object.
    QueryOperationResponse<Customer> response = 
        query.Execute() as QueryOperationResponse<Customer>;

    // Retrieve the total count from the response.
    Console.WriteLine("There are a total of {0} customers.", response.TotalCount);

    // Enumerate the customers in the response.
    foreach (Customer customer in response)
    {
        Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

次の例では、Customers エンティティ セット内のエンティティの数である整数値のみを返す Count メソッドを呼び出します。

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define a new query for Customers.
Dim query As DataServiceQuery(Of Customer) = context.Customers

Try
    ' Execute the query to just return the value of all customers in the set.
    Dim totalCount = query.Count()

    ' Retrieve the total count from the response.
    Console.WriteLine("There are {0} customers in total.", totalCount)
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);
         
// Define a new query for Customers.
DataServiceQuery<Customer> query = context.Customers;

try
{    
    // Execute the query to just return the value of all customers in the set.
    int totalCount = query.Count();
    
    // Retrieve the total count from the response.
    Console.WriteLine("There are {0} customers in total.", totalCount);
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

参照

概念

データ サービスのクエリ (WCF Data Services)