Freigeben über


Gewusst wie: Bestimmen der Anzahl von Entitäten, die von einer Abfrage zurückgegeben werden (WCF Data Services)

Mit WCF Data Services können Sie die Anzahl von Entitäten bestimmen, die in der durch einen Abfrage-URI angegebenen Entitätenmenge enthalten sind. Diese Anzahl kann entweder zusammen mit dem Abfrageergebnis oder als ganzzahliger Wert einbezogen werden. Weitere Informationen finden Sie unter Abfragen des Datendiensts (WCF Data Services).

Im Beispiel in diesem Thema werden der Northwind-Beispieldatendienst und automatisch generierte Clientdatendienstklassen verwendet. Dieser Dienst und die Clientdatenklassen werden erstellt, wenn Sie den WCF Data Services-Schnellstart ausführen.

Beispiel

In diesem Beispiel wird eine Abfrage ausgeführt, nachdem die IncludeTotalCount-Methode aufgerufen wurde. Die TotalCount-Eigenschaft gibt die Anzahl der in der Customers-Entitätenmenge enthaltenen Entitäten zurück.

' 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(vbTab & "Customer 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);
}

In diesem Beispiel wird die Count-Methode aufgerufen, um nur einen ganzzahligen Wert zurückzugeben, der die Anzahl der Entitäten in der Customers-Entitätenmenge angibt.

' 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);
}

Siehe auch

Konzepte

Abfragen des Datendiensts (WCF Data Services)