Defining FetchPlans Per Query

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

You could use the LoadWith or Include methods to define a fetch plan per query. Not that you could not mix both methods.

The Include and LoadWith<T> extension methods are from the Telerik.OpenAccess namespace.

It is not possible to mix the Include and LoadWith methods in one query.

The first step before using them is to add the Telerik.OpenAccess using (Import) statement.

using Telerik.OpenAccess;
Imports Telerik.OpenAccess

Using the LoadWith Method

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);
   IQueryable<Customer> query = from c in dbContext.Customers.LoadWith(fetchStrategy)
                                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)
 Dim query As IQueryable(Of Customer) = From c In dbContext.Customers.LoadWith(fetchStrategy)
                                        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 Include Method

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

using (FluentModel dbContext = new FluentModel())
{
   IQueryable<Customer> query = from c in dbContext.Customers.Include(c => c.Orders)
                                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 query As IQueryable(Of Customer) = From c In dbContext.Customers.Include(Function(c) c.Orders)
                                        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