How to: Retrieve Many Objects At Once

You can retrieve many objects in one query by using LoadWith.

The following code uses the LoadWith method to retrieve both Customer and Order objects.

C#


    using (NorthwindDbContext dbContext = new NorthwindDbContext())
    {
       Telerik.OpenAccess.FetchOptimization.FetchStrategy fetchStrategy = new Telerik.OpenAccess.FetchOptimization.FetchStrategy();
       fetchStrategy.LoadWith<Customer>(c => c.Orders);
       dbContext.FetchStrategy = fetchStrategy;
    
       IQueryable<Customer> query = from c in dbContext.Customers
                                       where c.Country == "Germany"
                                       select c;
    
       foreach (Customer customer in query)
       {
           Console.WriteLine(customer.CustomerID);
           foreach (Order order in customer.Orders)
           {
               Console.WriteLine("\t{0}", order.OrderID);
           }
       }
    }

VB


    Using dbContext As New NorthwindDbContext()
     Dim fetchStrategy As New Telerik.OpenAccess.FetchOptimization.FetchStrategy()
     fetchStrategy.LoadWith(Of Customer)(Function(c) c.Orders)
     dbContext.FetchStrategy = fetchStrategy
    
     Dim query As IQueryable(Of Customer) = From c In dbContext.Customers
                                            Where c.Country = "Germany"
                                            Select c
    
     For Each customer_Renamed As Customer In query
      Console.WriteLine(customer_Renamed.CustomerID)
      For Each order_Renamed As Order In customer_Renamed.Orders
       Console.WriteLine(vbTab & "{0}", order_Renamed.OrderID)
      Next order_Renamed
     Next customer_Renamed
    End Using

See Also