LinqToSql Update code model : does it influence performace ? If the Entity has many sub items... RRS feed

  • Question


    I use LinqToSQL to buttress a big project. However when I dig into Linq   , I feel buffled in some aspects. Now I just want to mention one of them.


    Let's start with a concret sample. In the project I have an entity named "Quotation" , which holds some sub-items collection , such as Element , Operation , each of which also relates to deeper link relationships.

    That means :


    If I initialize  Quotation q   ,  q may have :  q.Elements ,  q.Operations ....

     for each Element elem in q.Elements ,   elem has : elem.Currency , elem.Category ( Currency , Category are objects refered by Element )


    Now ,  I just want to simply update Quotation object .From many tutorials posted on web , I write the code:


    Code Snippet

    DataContext db = new DataContext();

    int quotationId = 5;

    Quotation quotation = db.Quotations.Single( q => q.QuotationId = quotationId );


    quotation.Price = 240;







    You can see that here I just intend to do a simple update against an assigned Quotation object; However when I debug the code I find such a "retrieval" is so "heavy" that all the relavant sub items will be dragged out together : all the Elements, each Element's Currency / Category entities , etc...   Because when I scrutinize the variables and properties , I found  "EntitySet" , "EntityRef" all having values.  Acutually in such a simple updating I really do not need the damn children collection. Such a manner  obviously damagely affects performance .


    I know LinqToSQL DBML tools can set a "LazyLoading" property but it just aims at the "value type property" of an entity object itself , I dont know how to prevent base frame from getting an object's sub items informations when user applying "Single"-like ( Element operator )  operations. 


    Please give me some tips, thank you !





    Sunday, May 11, 2008 7:31 AM



    Hi Atlas,


    In the code snippet that you have written, the child entitysets and entities will not be loaded.I think the reason that you are seeing values in EntityRef s and EntitySets is, in debug time when you are trying to access to the entity set, since you have accessed the object the lazy loading performs and loads the object, therefore you'll see the value in there.


    Try to Set context.Log property to some file stream or Console.Out to see what queries are generated. Hopefully you will see only the quotation table to be queried when a single() occurs.




    Sunday, May 11, 2008 9:16 AM