7

Does anyone have a simple code example in linq2sql that demonstrates the difference between Eager Loading and Lazy Loading?

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Brendan
  • 1,486
  • 3
  • 19
  • 27
  • 5
    LINQ is just a set of extension methods and language syntax. You probably mean Entity Framework or LINQ to SQL, or perhaps _deferred execution_. – SLaks Oct 23 '11 at 00:18
  • @SLaks Yes I meant Lin2Sql. I've updated the question. Now I am confused :-) I thought deferred execution was lazy loading? I guess this is a separate question. – Brendan Oct 23 '11 at 00:25
  • 1
    Deferred Execution is where a query is not executed until it's actually used. Lazy-Loading is sort of the same, except your deferring the loading of an association until it's actually needed. This is a good link for understanding deferred execution: http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx – Phill Oct 23 '11 at 00:33
  • @zener - No problem, do you need help with Eager/Lazy loading in L2S? Or understanding what the difference between the two are? – Phill Oct 23 '11 at 01:11
  • @Phil I was looking to understand when you would use either, hopefully a code example would help explain. – Brendan Oct 23 '11 at 08:13

1 Answers1

10
  • Deferred Loading: for a given entity, it's associated collections may be empty when it is first loaded, but when these collections are first iterated, LINQ to SQL fires off a query to load these collections after it is loaded the collection is then available for future use without requiring another query:

    var query = from o in db.GetTable<Order>() //db is the datacontext
            select o;
    foreach(Order o in query)
    {
        foreach(OrderDetail d in o.OrderDetails)//Deferred loading
        {
           //Do something on these collections
        }
    }
    

The OrderDetails are loaded only if it is iterated, so If the OrderDetatils are never iterated the corresponding query is never executed.

  • Eager Loading: immediate loading the associated Collections for all referenced entities for example LINQ to SQL will automatically brings all the OrderDetails for all the retreived Orders

    DataLoadOptions op = new DataLoadOptions();
    op.LoadWith<Order>(o => o.OrderDetails);
    db.LoadOptions = op;
    var query = from o in db.GetTable<Order>() 
             select o;
    foreach(Order o in query)
    { 
        //Order details are eager loaded; additional queries are not needed
        foreach(OrderDetail d in o.OrderDetails)
        {
           //do something
        }
    }
    

note that: Deferred execution is a LINQ Feature but Deferred Loading is a LINQ to SQL feature

Protector one
  • 6,926
  • 5
  • 62
  • 86
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
  • 2
    'Deffered Loading' is Lazy Loading and is a feature of pretty much every ORM. – Phill Oct 23 '11 at 02:31
  • @Phill of course it is a feature for almost every ORM but i add this as a note to be a distinction between them as all the comments above pointed to deffered execution but the OP question was about the deffered Loading so in this context it is valid – Mahmoud Gamal Oct 23 '11 at 03:01
  • I know, my point is it's the same thing, just L2S call it something different. So for someone coming along to this question and seeing the answer going "why is he talking about deferred execution", they know it's just Lazy Loading with a different name in L2S. – Phill Oct 23 '11 at 04:25