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

Defining FetchPlans Per Context

Telerik Data Access allows you to define FetchPlans either per context or per query. This topic shows you how to define FetchPlans on a context level.

You could use the LoadWith method to define a fetch plan per context. The LoadWith method is used to retrieve specified data related to the main target. There are two LoadWith methods - generic and non-generic. Both methods are covered in this topic.

  • LoadWith<T>(Expression<Func<T, Object>>) - specifies which sub-objects to retrieve when a query is submitted for an object of type T. Read more
  • LoadWith(LambdaExpression) - retrieves specified data related to the main target by using a lambda expression. Read more

Using the LoadWith<T> Method

The LoadWith<T> method specifies which sub-objects to be retrieved when a query is submitted for an object of type T.

You cannot specify the loading of two levels of relationships (for example, Orders.OrderDetails). In these scenarios you must specify two separate LoadWith methods.

In the following example, all the Orders 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 (FluentModel dbContext = new FluentModel())
{
   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 Id: {0}", customer.CustomerID);
       foreach (Order order in customer.Orders)
       {
           Console.WriteLine("===Order date: {0}", order.OrderDate.ToString());
       }
   }
}
Using dbContext As New FluentModel()
 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 As Customer In query
  Console.WriteLine("Customer Id: {0}", _customer.CustomerID)
  For Each _order As Order In _customer.Orders
   Console.WriteLine("===Order date: {0}", _order.OrderDate.ToString())
  Next _order
 Next _customer
End Using

Using the LoadWith Method

The LoadWith method retrieves specified data related to the main target by using a lambda expression.

You cannot specify the loading of two levels of relationships (for example, Orders.OrderDetails). In these scenarios you must specify two separate LoadWith methods.

The following code-snippet demonstrates again the "LoadWith<T> example", but this time the non-generic method is used:

using (FluentModel dbContext = new FluentModel())
{
   Telerik.OpenAccess.FetchOptimization.FetchStrategy fetchStrategy = new 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 Id: {0}", customer.CustomerID);
       foreach (Order order in customer.Orders)
       {
           Console.WriteLine("===Order date: {0}", order.OrderDate.ToString());
       }
   }
}
Using dbContext As New FluentModel()
 Dim _fetchStrategy As New Telerik.OpenAccess.FetchOptimization.FetchStrategy()
 _fetchStrategy.LoadWith(Function(c As Customer) 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 As Customer In query
  Console.WriteLine("Customer Id: {0}", _customer.CustomerID)
  For Each _order As Order In _customer.Orders
   Console.WriteLine("===Order date: {0}", _order.OrderDate.ToString())
  Next _order
 Next _customer
End Using