This is a migrated thread and some comments may be shown as answers.

linq basic question

1 Answer 48 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sebastien AUROUX
Top achievements
Rank 1
Sebastien AUROUX asked on 12 Oct 2010, 12:45 PM
I am new in open access, and i dont really understand linq query in open access.
in the documentation http://www.telerik.com/help/openaccess-orm/getting-started-root-getting-started-with-fetchplans.html
you tell that linq query is executed for each loop. If i want to query my Business Objects to do 10 inner joins why don't you create only one sql query to make those 10 inner joins to get the wanted data ? because to do so, if i use the fetch plan mecanism will i retrieve only the wanted data

thanks
sebastien

1 Answer, 1 is accepted

Sort by
0
Petko_I
Telerik team
answered on 15 Oct 2010, 07:20 AM
Hello Sebastien AUROUX,

Let me try to explain the example:

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("d"));
       foreach (OrderDetail orderDetail in order.OrderDetails)
       {
           Console.WriteLine("======Unit price: {0}", orderDetail.UnitPrice.ToString("c"));
       }
   }
}

With the default lazy loading when you get a customer (the first loop) no Orders are loaded for that customer. In the second loop (first inner one) the Orders property on the customer persistent object is populated from the database with a query. The very access to the property triggers the retrieval of the persistent objects in memory. In general, you cannot predict what you are going to do with every customer in the loop and that is why the default behavior delays the retrieval of related persistent objects until they are absolutely needed. So, if you have 10 customers and for each one you access their Orders, you will have 10 queries that populate the orders. There are quite a lot of scenarios with which preloading related objects is not only unnecessary but provides an additional overhead. If you wish to work only with the names of the customers, it makes no sense to fire queries to the database to get data you will not use. That is why the Fetch Optimization API provides an alternative for the users to decide whether the eager retrieval will benefit them in some way.

The Fetch Optimization API comes in handy in situations when you are completely confident that you will access related objects irrespective of the circumstances. Here is a blog post which elaborates on this topic.

Do not hesitate to contact us, if you have any doubts regarding the usage of the Fetch Optimization API.

Regards,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
LINQ (LINQ specific questions)
Asked by
Sebastien AUROUX
Top achievements
Rank 1
Answers by
Petko_I
Telerik team
Share this question
or