Data Access has been discontinued. Please refer to this page for more information.

How to: Use Multiple Fetch Definitions

When you use the LoadWith method, it is not allowed to specify the loading of two levels of relationships (e.g, Orders.OrderDetails). In these scenarios you must specify two separate LoadWith methods.

In the following example, all the Orders and OrderDetails for all the Customers who are located in Germany are retrieved when the query is executed. As a result, successive access to the Orders property on a Customer object does not trigger a new database query.

using (NorthwindDbContext dbContext = new NorthwindDbContext())
{
   Telerik.OpenAccess.FetchOptimization.FetchStrategy fetchStrategy = 
        new Telerik.OpenAccess.FetchOptimization.FetchStrategy();
   fetchStrategy.LoadWith<Customer>(c => c.Orders);
   fetchStrategy.LoadWith<Order>(o => o.OrderDetails);

   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 Id: {0}", customer.CustomerID);
       foreach (Order order in customer.Orders)
       {
           Console.WriteLine("===Order date: {0}", order.OrderDate.ToString());
           foreach (OrderDetail orderDetail in order.OrderDetails)
           {
               Console.WriteLine("======Unit price: {0}", orderDetail.UnitPrice.ToString("c"));
           }
       }
   }
}
Using dbContext As New NorthwindDbContext()
 Dim fetchStrategy As New Telerik.OpenAccess.FetchOptimization.FetchStrategy()
 fetchStrategy.LoadWith(Of Customer)(Function(c) c.Orders)
 fetchStrategy.LoadWith(Of Order)(Function(o) o.OrderDetails)

 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 Id: {0}", customer_Renamed.CustomerID)
  For Each order_Renamed As Order In customer_Renamed.Orders
   Console.WriteLine("===Order date: {0}", order_Renamed.OrderDate.ToString())
   For Each orderDetail_Renamed As OrderDetail In order_Renamed.OrderDetails
    Console.WriteLine("======Unit price: {0}", orderDetail_Renamed.UnitPrice.ToString("c"))
   Next orderDetail_Renamed
  Next order_Renamed
 Next customer_Renamed
End Using

Alternatively, you can use the second overload of the LoadWith<T> method, accepting an array of expression in the following manner.

fetchStrategy.LoadWith<Order>(c => c.OrderDetails, c => c.Customer);
fetchStrategy.LoadWith(Of Order)(Function(c) c.OrderDetails, Function(c) c.Customer)

See Also