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. Um den Code in diesem Beispiel auszuführen, müssen Sie Ihrem Projekt bereits das AdventureWorks Sales-Modell hinzugefügt und das Projekt zur Verwendung von Entity Framework konfiguriert haben. Führen Sie dazu die Verfahren unter Gewusst wie: Manuelles Konfigurieren eines Entity Framework-Projekts und Gewusst wie: Manuelles Definieren eines Entity Data Model (Entity Framework) durch. 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 Assistenten für Entity Data Model (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 AdventureWorksEntities = _
New AdventureWorksEntities()
Try
' Get a specified customer by contact ID.
Dim customer = (From customers In context.Contact _
Where customers.ContactID = customerId _
Select customers).First()
' Load the customer orders if not already loaded.
If Not customer.SalesOrderHeader.IsLoaded Then
customer.SalesOrderHeader.Load()
End If
' Write the number of orders for the customer.
Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
customer.LastName, customer.SalesOrderHeader.Count)
' Get the online orders that have shipped.
Dim shippedOrders = _
From order In customer.SalesOrderHeader _
Where order.OnlineOrderFlag = True _
And order.Status = 5 _
Select order
' Write the number of orders placed online.
Console.WriteLine("{0} orders placed online have been shipped.", _
shippedOrders.Count())
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
// Specify the customer ID.
int customerId = 4332;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Get a specified customer by contact ID.
var customer = (from customers in context.Contact
where customers.ContactID == customerId
select customers).First();
// Load the customer orders if not already loaded.
if (!customer.SalesOrderHeader.IsLoaded)
{
customer.SalesOrderHeader.Load();
}
// Write the number of orders for the customer.
Console.WriteLine("Customer '{0}' has placed {1} total orders.",
customer.LastName, customer.SalesOrderHeader.Count);
// Get the online orders that have shipped.
var shippedOrders =
from order in customer.SalesOrderHeader
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());
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
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 AdventureWorksEntities = _
New AdventureWorksEntities()
Try
' Get a specified customer by contact ID.
Dim customer = (From customers In context.Contact _
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.SalesOrderHeader.CreateSourceQuery() _
Where orders.OnlineOrderFlag = True _
And orders.Status = 5 _
Select orders
' Write the number of orders placed online.
Console.WriteLine("{0} orders placed online have been shipped.", _
shippedOrders.Count())
' Load the remaining orders for this customer.
If Not customer.SalesOrderHeader.IsLoaded Then
customer.SalesOrderHeader.Load()
End If
' Write the number of total orders for the customer.
Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
customer.LastName, customer.SalesOrderHeader.Count)
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
// Specify the customer ID.
int customerId = 4332;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Get a specified customer by contact ID.
var customer = (from customers in context.Contact
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.SalesOrderHeader.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());
// Load the remaining orders for this customer.
if (!customer.SalesOrderHeader.IsLoaded)
{
customer.SalesOrderHeader.Load();
}
// Write the number of total orders for the customer.
Console.WriteLine("Customer '{0}' has placed {1} total orders.",
customer.LastName, customer.SalesOrderHeader.Count);
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
Siehe auch
Aufgaben
Gewusst wie: Ausführen einer Abfrage, die einen Entitätstyp zurückgibt (Entity Framework)
Gewusst wie: Bestimmen von Ergebnissen mit Abfragepfaden (Entity Framework)
Gewusst wie: Navigieren in Beziehungen mithilfe von Navigationseigenschaften (Entity Framework)