Gewusst wie: Abfragen von verbundenen Objekten in einer EntityCollection (Entity Framework)

In diesem Thema finden Sie Beispiele für das Abfragen verbundener Objekte in einer EntityCollection, die von der Navigationseigenschaft der Beziehung zurückgegeben wurde.

Das Beispiel in diesem Thema beruht auf dem AdventureWorks Sales-Modell. Zum Ausführen des Codes in diesem Beispiel muss dem Projekt bereits das AdventureWorks Sales-Modell hinzugefügt und das Projekt für die Verwendung von Entity Framework konfiguriert worden sein. Verwenden Sie dazu das Verfahren aus Gewusst wie: Manuelles Konfigurieren eines Entity Framework-Projekts und Gewusst wie: Manuelles Definieren der Modell- und Zuordnungsdateien (Entity Framework). Sie können auch den Assistenten für Entity Data Model zum Definieren des AdventureWorks Sales-Modells verwenden. Weitere Informationen finden Sie unter Gewusst wie: Verwenden des Entity Data Model-Assistenten (Entity Framework).

Beispiel

In diesem Beispiel wird die Auflistung von SalesOrderHeader-Objekten geladen, die mit einem bestimmten Kontakt verbunden sind. Anschließend wird mithilfe eines LINQ-Ausdrucks eine Liste mit online durchgeführten und bereits versendeten Bestellungen zurückgegeben.

' Specify the customer ID. 
Dim customerId As Integer = 4332

Using context As New AdventureWorksEntities()
    ' Get a specified customer by contact ID. 
    Dim customer = (From customers In context.Contacts _
        Where customers.ContactID = customerId _
        Select customers).First()

    ' You do not have to call the Load method to load the orders for the customer, 
    ' because lazy loading is set to true 
    ' by the constructor of the AdventureWorksEntities object. 
    ' With lazy loading set to true the related objects are loaded when 
    ' you access the navigation property. In this case SalesOrderHeaders. 

    ' Write the number of orders for the customer. 
    Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
                      customer.LastName, customer.SalesOrderHeaders.Count)

    ' Get the online orders that have shipped. 
    Dim shippedOrders = From order In customer.SalesOrderHeaders _
        Where order.OnlineOrderFlag = True AndAlso order.Status = 5 _
        Select order

    ' Write the number of orders placed online. 
    Console.WriteLine("{0} orders placed online have been shipped.", shippedOrders.Count())
End Using
// Specify the customer ID.
int customerId = 4332;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Get a specified customer by contact ID.
    var customer = (from customers in context.Contacts
                    where customers.ContactID == customerId
                    select customers).First();

    // You do not have to call the Load method to load the orders for the customer,
    // because  lazy loading is set to true 
    // by the constructor of the AdventureWorksEntities object. 
    // With  lazy loading set to true the related objects are loaded when
    // you access the navigation property. In this case SalesOrderHeaders.

    // Write the number of orders for the customer.
    Console.WriteLine("Customer '{0}' has placed {1} total orders.",
        customer.LastName, customer.SalesOrderHeaders.Count);

    // Get the online orders that have shipped.
    var shippedOrders =
        from order in customer.SalesOrderHeaders
        where order.OnlineOrderFlag == true
        && order.Status == 5
        select order;

    // Write the number of orders placed online.
    Console.WriteLine("{0} orders placed online have been shipped.",
        shippedOrders.Count());
}

In diesem Beispiel wird die gleiche LINQ-Abfrage für die Auflistung von SalesOrderHeader-Objekten wie im ersten Beispiel verwendet. Statt gleich zu Anfang alle verbundenen Objekte in die Auflistung zu laden, wird die CreateSourceQuery-Methode verwendet, um nur die von der Abfrage zurückgegebenen Objekte zu laden. Dann wird zum Laden der übrigen verbundenen Objekte die Load-Methode für die EntityCollection aufgerufen, die von der Navigationseigenschaft der SalesOrderHeader-Beziehung zurückgegeben wurde.

' Specify the customer ID. 
Dim customerId As Integer = 4332

Using context As New AdventureWorksEntities()
    ' Get a specified customer by contact ID. 
    Dim customer = (From customers In context.Contacts
        Where customers.ContactID = customerId
        Select customers).First()

    ' Use CreateSourceQuery to generate a query that returns 
    ' only the online orders that have shipped. 
    Dim shippedOrders = From orders In customer.SalesOrderHeaders.CreateSourceQuery() _
        Where orders.OnlineOrderFlag = True AndAlso orders.Status = 5 _
        Select orders

    ' Write the number of orders placed online. 
    Console.WriteLine("{0} orders placed online have been shipped.", shippedOrders.Count())

    ' You do not have to call the Load method to load the orders for the customer, 
    ' because lazy loading is set to true 
    ' by the constructor of the AdventureWorksEntities object. 
    ' With lazy loading set to true the related objects are loaded when 
    ' you access the navigation property. In this case SalesOrderHeaders. 

    ' Write the number of total orders for the customer. 
    Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
                      customer.LastName, customer.SalesOrderHeaders.Count)
End Using
// Specify the customer ID.
int customerId = 4332;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Get a specified customer by contact ID.
    var customer = (from customers in context.Contacts
                    where customers.ContactID == customerId
                    select customers).First();

    // Use CreateSourceQuery to generate a query that returns 
    // only the online orders that have shipped.
    var shippedOrders =
        from orders in customer.SalesOrderHeaders.CreateSourceQuery()
        where orders.OnlineOrderFlag == true
        && orders.Status == 5
        select orders;

    // Write the number of orders placed online.
    Console.WriteLine("{0} orders placed online have been shipped.",
        shippedOrders.Count());

    // You do not have to call the Load method to load the orders for the customer,
    // because  lazy loading is set to true 
    // by the constructor of the AdventureWorksEntities object. 
    // With  lazy loading set to true the related objects are loaded when
    // you access the navigation property. In this case SalesOrderHeaders.

    // Write the number of total orders for the customer.
    Console.WriteLine("Customer '{0}' has placed {1} total orders.",
        customer.LastName, customer.SalesOrderHeaders.Count);
}

Siehe auch

Aufgaben

Gewusst wie: Ausführen einer Abfrage, die Entitätstypobjekte zurückgibt (Entity Framework)
Gewusst wie: Bestimmen von Ergebnissen mit Abfragepfaden (Entity Framework)
Gewusst wie: Navigieren in Beziehungen mithilfe von Navigationseigenschaften (Entitiy Framework)

Konzepte

Laden von verknüpften Objekten (Entity Framework)