Share via


Gewusst wie: Laden von verknüpften Entitäten (WCF Data Services)

Wenn Sie zugeordnete Entitäten in WCF Data Services laden müssen, können Sie die LoadProperty-Methode für die DataServiceContext-Klasse verwenden. Sie können auch die Expand-Methode für die DataServiceQuery<TElement> verwenden, um festzulegen, dass verknüpfte Entitäten unverzüglich in die gleiche Abfrageantwort geladen werden.

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

Im folgenden Beispiel wird gezeigt, wie der Customer, der sich auf jede zurückgegebene Orders-Instanz bezieht, explizit geladen wird.

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

Try
    ' Enumerate over the top 10 orders obtained from the context.
    For Each order As Order In context.Orders.Take(10)
        ' Explicitly load the customer for each order.
        context.LoadProperty(order, "Customer")

        ' Write out customer and order information.
        Console.WriteLine("Customer: {0} - Order ID: {1}", _
                order.Customer.CompanyName, order.OrderID)
    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);

try
{
    // Enumerate over the top 10 orders obtained from the context.
    foreach (Order order in context.Orders.Take(10))
    {
        // Explicitly load the customer for each order.
        context.LoadProperty(order, "Customer");

        // Write out customer and order information.
        Console.WriteLine("Customer: {0} - Order ID: {1}", 
            order.Customer.CompanyName, order.OrderID);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

Im folgenden Beispiel wird die Verwendung der Expand-Methode verwendet wird, um Order Details zurückzugeben, die zu den von der Abfrage zurückgegebenen Orders gehören.

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

' Define a query for orders that also returns items and customers.
Dim query As DataServiceQuery(Of Order) = _
context.Orders.Expand("Order_Details,Customer")

Try
    ' Enumerate over the first 10 results of the query.
    For Each order As Order In query.Take(10)
        Console.WriteLine("Customer: {0}", order.Customer.CompanyName)
        Console.WriteLine("Order ID: {0}", order.OrderID)

        For Each item As Order_Detail In order.Order_Details
            Console.WriteLine(vbTab & "Product: {0} - Quantity: {1}", _
                    item.ProductID, item.Quantity)
        Next
    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 query for orders that also returns items and customers.
DataServiceQuery<Order> query =
    context.Orders.Expand("Order_Details,Customer");

try
{
    // Enumerate over the first 10 results of the query.
    foreach (Order order in query.Take(10))
    {
        Console.WriteLine("Customer: {0}", order.Customer.CompanyName);
        Console.WriteLine("Order ID: {0}", order.OrderID);

        foreach (Order_Detail item in order.Order_Details)
        {
            Console.WriteLine("\tProduct: {0} - Quantity: {1}",
                item.ProductID, item.Quantity);
        }
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

Siehe auch

Konzepte

Abfragen des Datendiensts (WCF Data Services)