2

I have a read-only database, so I am turning off ObjectTracking (thus implicitly turning off DeferredLoading).

I wish to do lazy loading and not use LoadWith<>.

What is the simplest way to explicitly tell Linq to go and lazy fetch a relation just before I need the data itself.

For example: a simple dbml alt text

If I have the following code:

  TestDbDataContext context = new TestDbDataContext(Settings.Default.TestersConnectionString);
  context.ObjectTrackingEnabled = false;

  var result = context.Employees.ToList();
  foreach (var employee in result)
  {
    // HERE Should load gift list
    foreach (var gift in employee.Gifts)
    {
      Console.WriteLine(gift.Name);
    }
  }

I know I can write a full query again, but I hope we can find together a better way.

2 Answers 2

2

You are fighting the system... 2 thoughts:

  • if you know you need the other data (nested foreach), why wouldn't you want to use LoadWith? That is pretty-much the text-book use case
  • since you (from post) know that object tracking is required for lazy loading, why not just enable object tracking; data-contexts should usually be considered "units of work" (i.e. short-lived), so this isn't likely to hurt much in reality.

See the official replies here for why these two options (object tracking and deferred loading) are linked.

Sign up to request clarification or add additional context in comments.

25 Comments

The code you see is a much simplified version of my real code. My code first assumed LoadWith, now I'm trying to optimize and lazy load instead of eager load (because of performance), so just before I really need the data I want want to load it. It may really leave the scope of the datacontext...
and data will be passed through the application.
Are you actually suer, from profiling, that ObjectTracking is causing you a problem. Given your requirements, I'd just enable it. Personally, though, I like a repository pattern with no lazy loading - my repository fetches data (or updates data) within a method and then has nothing to do with it...
The problem is that if I've got plenty of relations (I've ended with 17 LoadWith<> statements) loading a single entity can take much time. what should I do?
BTW about repository pattern. Do you consider this article as valid guide for implementing with Linq2Sql ? codeproject.com/KB/architecture/linqrepository.aspx
|
0

Use a LazyList: http://blog.wekeroad.com/blog/lazy-loading-with-the-lazylist/

4 Comments

Yeah, I know about that one. But this one demands major rewrites of the generated entities and queries.
From experience, having your own data model beats counting on Linq2Sql... (too many advantages to list here)
Worth thought but irrelevant.
Havent played much with the Linq2Sql autogenerated class but maybe you can get it to NOT generate the Gifts property and then add a partial class to Employee and define that property yourself (using IQueryable rather than EntitySet or whatever Linq2Sql uses)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.