In LINQ-SQL, wrap the DataContext is an using statement - pros cons

Posted by hIpPy on Stack Overflow See other posts from Stack Overflow or by hIpPy
Published on 2010-02-18T21:50:03Z Indexed on 2010/03/19 19:21 UTC
Read the original article Hit count: 443

Filed under:
|
|

Can someone pitch in their opinion about pros/cons between wrapping the DataContext in an using statement or not in LINQ-SQL in terms of factors as performance, memory usage, ease of coding, right thing to do etc.

Update: In one particular application, I experienced that, without wrapping the DataContext in using block, the amount of memory usage kept on increasing as the live objects were not released for GC. As in, in below example, if I hold the reference to List of q object and access entities of q, I create an object graph that is not released for GC.

DataContext with using

    using (DBDataContext db = new DBDataContext())
    {
        var q = 
            from x in db.Tables
            where x.Id == someId
            select x;

        return q.toList();
    }

DataContext without using and kept alive

  DBDataContext db = new DBDataContext()
  var q = 
        from x in db.Tables
        where x.Id == someId
        select x;

    return q.toList(); 

Thanks.

© Stack Overflow or respective owner

Related posts about datacontext

Related posts about LINQ