Welcome to Bilal Haidar [MVP, MCT] Official Blog Sign in | Join | Help

Deferred Loading in DLinq

Suppose we have an entity class called Customer and it contains a property of type List of Order entity classes. Now we want to retrieve all the customers and their orders, how do we do this using Query Expression?

var results=  from c in db.Customer
  select c;

However, this will not load the orders inside the customer entities returned! Why? The reason is that DLinq uses deferred loading and is known as "lazy loading".
In the above query, we only retrieved the customer entities, but the Orders were not loaded too! This goes back to the fact that Customer class has a relation with the Order class just as the relation between Customer and Order data tables. in the database. This explains that related objects are governed by "Deferred loading". Only the main class's properties are loaded, and all related objects are not.

But, when you enumerate through the results, and when you access the Orders property, DLinq goes to the database and retrieves them without letting you know of this!

So, what if we wanted to disable Deferred Loading? You could do a projection and retrieve both elements, but then the result of the projection will be read only. This is where we use DataShape object!!

 DataShape ds = new DataShape();
 ds.LoadWith<Customer>(c => c.Orders);
 db.Shape = ds;
 var q =
  from c in db.Customers
  where c.City == "London"
  select c;

You are informing the query to load the Orders collection on the Customer class, whenever a customer entity is loaded from the database!

Hope this helps,
Regards

Published Thursday, July 26, 2007 4:08 AM by BilalHaidar [MVP]
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Deferred Loading in DLinq - Orcas Beta 2

In a previous post of mine (Deferred Loading in DLinq - http://bhaidar.net/cs/archive/2007/07/26/deferred-loading-in-dlinq.aspx

Monday, August 13, 2007 3:34 PM by Bilal Haidar [MVP, MCT]

# re: Deferred Loading in DLinq

it does help.

Saturday, November 01, 2008 1:26 PM by otineakr

Leave a Comment

(required) 
required 
(required)