linq basic question

2 posts, 0 answers
  1. Sebastien AUROUX
    Sebastien AUROUX avatar
    34 posts
    Member since:
    Sep 2008

    Posted 12 Oct 2010 Link to this post

    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
  2. Petko_I
    Admin
    Petko_I avatar
    184 posts

    Posted 15 Oct 2010 Link to this post

    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
  3. DevCraft banner
Back to Top