Partager via


Procédure : Récupérer plusieurs objets à la fois

Vous pouvez récupérer plusieurs objets dans une requête en utilisant LoadWith.

Exemple

Le code suivant utilise la méthode LoadWith pour récupérer des objets Customer et Order.

Northwnd db = new Northwnd(@"northwnd.mdf");
DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<Customer>(c => c.Orders);
ds.LoadWith<Order>(o => o.OrderDetails);
db.LoadOptions = ds;

var custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach (Customer custObj in custQuery)
{
    Console.WriteLine("Customer ID: {0}", custObj.CustomerID);
    foreach (Order ord in custObj.Orders)
    {
        Console.WriteLine("\tOrder ID: {0}", ord.OrderID);
        foreach (OrderDetail detail in ord.OrderDetails)
        {
            Console.WriteLine("\t\tProduct ID: {0}", detail.ProductID);
        }
    }
}
Dim db As New Northwnd("c:\northwnd.mdf")
Dim ds As DataLoadOptions = New DataLoadOptions()
ds.LoadWith(Of Customer)(Function(c) c.Orders)
ds.LoadWith(Of Order)(Function(o) o.OrderDetails)
db.LoadOptions = ds

Dim custQuery = From cust In db.Customers() _
                Where cust.City = "London" _
                Select cust

For Each custObj In custQuery
    Console.WriteLine("Customer ID: " & custObj.CustomerID)

    For Each ord In custObj.Orders
        Console.WriteLine(vbTab & "Order ID: " & ord.OrderID)

        For Each detail In ord.OrderDetails
            Console.WriteLine(vbTab & vbTab & _
                        "Product ID: {0}", detail.ProductID)
        Next
    Next
Next

Voir aussi