The LINQ framework provide a simple (although not a mature) solution to eager loading data, namely the DataLoadOptions class. This class allows you to specify what related data should be retrieved when some data X is retrieved, for example you can tell the data context to load all related apartments when loading a building:
DataLoadOptions options = new System.Data.Linq.DataLoadOptions(); options.LoadWith<Building>(building => building.Apartments); _DataContext = new RdmDataContext(); _DataContext.LoadOptions = options;
This is great, whenever you request a building LINQ will automatically retrieve all the related apartments, there are some negatives to this though, so you need to think through your load options carefully.
Using the data load options above, if you request a list of buildings off the data context LINQ will hit the database just once to retrieve all the buildings and apartments so you will get the duplicate building information retrived for each apartment. Now this doesn’t affect your LINQ entities but it does mean you are retrieving more data than is needed. It would be nice if the guys at Microsoft could change the eager loading to first retrieve all the buildings and then a seperate query to retrieve all the related apartments.
You can’t request eager loading for more than one level in a single statement, so the statement below would not work:
options.LoadWith<Apartment>(apartment => apartment.CurrentLease.PrimaryTenant);
You need to break it into two statements:
options.LoadWith<Apartment>(apartment => apartment.CurrentLease); options.LoadWith<Lease>(lease => lease.PrimaryTenant);
If you have a scenario where some object has a collection of objects, and each of those objects has some collection of objects you can’t eager load it all at once. You’d think that the statement below would load all apartments and leases when you retrieve a building, but it doesn’t.
options.LoadWith<Building>(building => building.Apartments); options.LoadWith<Apartment>(apartment => apartment.Leases);
This will retrieve the leases on the apartment as needed, i.e. lazy load. I’m not sure if this is a bug or a deliberate limitation, I haven’t been able to find any information on the issue.
Adding a calculated property to the data load options will be ignored so don’t bother, i.e. a property that is not mapped to a database field.
The last important item to note is that you need to assign the load options to the data context before requesting any data, and you can’t change the load options after requesting data from the data context. This is the biggest downside of the data load options.
Disclaimer: This post is based on Orcas CTP June 2007