none
Late binding in LINQ RRS feed

  • Question

  • Hi,

    In an recent interview, I was asked about late binding in LINQ.

    I could not understand the question and also googling could not helped me.

    Any ideas about late binding in LINQ ??


    San

    Friday, November 23, 2012 1:36 AM

Answers

  • Hi,

    it is deffered querying . It is of importance when you are using LINQ to SQL or LINQ to Entity when entity objects are not populated immediately when query is executed . It is populated when it is actually used in your code further .

    http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx

    • Marked as answer by Alexander Sun Friday, December 7, 2012 1:49 AM
    Saturday, November 24, 2012 8:28 AM
  • I suspect he meant Lazy evaluation/Lazy Loading.

    Lazy Evalaution

    Say you have the following collection -

    var Orders = new List<Order>(){ new Order(){ ID = 1, ItemCode ="Comp", Qty=3, UnitPrice = 300},
                                            new Order(){ ID = 2, ItemCode ="Mouse", Qty=5, UnitPrice = 100},
                                            new Order(){ ID = 3, ItemCode ="Comp", Qty=1, UnitPrice = 200},
                                            new Order(){ ID = 4, ItemCode ="Keyboard", Qty=4, UnitPrice = 250},
                                            new Order(){ ID = 5, ItemCode ="Comp", Qty=4, UnitPrice = 600}};

    Now you made a query as below -

                var query2 = from o in orders
                             where o.ItemCode == "Comp"
                             select o;


    Here’s million dollar Question: What does this query output? Nothing!

    Actually evalation in most cases is delayeduntil the result of the query is actally required. An approach known as Lazy evaluation.

    Meaning if you execute below line result will be 3 -

    var count2 = query2.Count();

    Now just Add attitional line befor above line and after var query2 statement.

    Orders.Add(new Order(){ ID = 8, ItemCode ="Comp", Qty=54, UnitPrice = 540});

    Now if you will see the result is 4.

    So, Lesson is LINQ query is just a query and is not evaluated utill called.

    Additional Information:

    One of the implication of Lazy evaluation is that query expressions are re-evaluated each time they are processed. In other words, the result of the query are not stored or cached in a collection, but lazily evalauated and returmed on each request. The advantage is that changes in data are automatically reflected in the next processing cycle.<o:p></o:p>

    If you want to force immediate result and cache it in a collection then you may call ToArray() or ToList() method or Count().<o:p></o:p>



    Dhananjay Tech Lead

    • Marked as answer by Alexander Sun Friday, December 7, 2012 1:49 AM
    Saturday, November 24, 2012 11:09 AM

All replies

  • Hi,

    I wish you had a debrief with the interviewer (it's always hard to guess what someone else had in mind). To me it's more likely a catch (the purpose of linq is to query data in a strongly typed manner so I see no interest in using late binding with Linq). The only case I can think of is with linq to dataset when you can cast to retrieve the data under its corresponding data type (http://msdn.microsoft.com/en-us/library/bb386910.aspx)


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, November 23, 2012 10:49 AM
  • Hi,

    it is deffered querying . It is of importance when you are using LINQ to SQL or LINQ to Entity when entity objects are not populated immediately when query is executed . It is populated when it is actually used in your code further .

    http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx

    • Marked as answer by Alexander Sun Friday, December 7, 2012 1:49 AM
    Saturday, November 24, 2012 8:28 AM
  • I suspect he meant Lazy evaluation/Lazy Loading.

    Lazy Evalaution

    Say you have the following collection -

    var Orders = new List<Order>(){ new Order(){ ID = 1, ItemCode ="Comp", Qty=3, UnitPrice = 300},
                                            new Order(){ ID = 2, ItemCode ="Mouse", Qty=5, UnitPrice = 100},
                                            new Order(){ ID = 3, ItemCode ="Comp", Qty=1, UnitPrice = 200},
                                            new Order(){ ID = 4, ItemCode ="Keyboard", Qty=4, UnitPrice = 250},
                                            new Order(){ ID = 5, ItemCode ="Comp", Qty=4, UnitPrice = 600}};

    Now you made a query as below -

                var query2 = from o in orders
                             where o.ItemCode == "Comp"
                             select o;


    Here’s million dollar Question: What does this query output? Nothing!

    Actually evalation in most cases is delayeduntil the result of the query is actally required. An approach known as Lazy evaluation.

    Meaning if you execute below line result will be 3 -

    var count2 = query2.Count();

    Now just Add attitional line befor above line and after var query2 statement.

    Orders.Add(new Order(){ ID = 8, ItemCode ="Comp", Qty=54, UnitPrice = 540});

    Now if you will see the result is 4.

    So, Lesson is LINQ query is just a query and is not evaluated utill called.

    Additional Information:

    One of the implication of Lazy evaluation is that query expressions are re-evaluated each time they are processed. In other words, the result of the query are not stored or cached in a collection, but lazily evalauated and returmed on each request. The advantage is that changes in data are automatically reflected in the next processing cycle.<o:p></o:p>

    If you want to force immediate result and cache it in a collection then you may call ToArray() or ToList() method or Count().<o:p></o:p>



    Dhananjay Tech Lead

    • Marked as answer by Alexander Sun Friday, December 7, 2012 1:49 AM
    Saturday, November 24, 2012 11:09 AM
  • This why it's IMO best to not be afraid to clarify with your interviewer rather than asking in a forum after the interview what he meant. Sometimes they don't even know themselves what they are talking about ;-).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Monday, November 26, 2012 10:13 AM