title: Cannot access a disposed object. Object name: 'DataContext accessed after Dispose
body: <p>The title is the exception that is thrown after I access joined objects after generating the data with the following method using LINQ2SQL:</p>
  
  <pre><code>public static bool Get(int skip, int take, ListSortDirection direction, out List&lt;Post&gt; posts)
  {
      bool result;
  
      using (var db = new MyDataContext())
      {
          try
          {
              posts = db.Posts
                  .Where(p =&gt; !p.IsDeleted)
                  .OrderBy(p =&gt; p.DateTimeCreated, direction)
                  .Skip(skip)
                  .Take(take)
                  .ToList();
              result = true;
          }
          catch (Exception)
          {
              // Log the exception
              posts = new List&lt;Post&gt;();
              result = false;
          }
      }
  
      return result;
  }
  </code></pre>
  
  <p>Below is an example of the join object that I'm referring to.  In the method above, I'm selecting a collection of posts and the userprofileID of the Userprofile table is joined (in the .dbml and the database server) to the UserprofileID of the Post table.  </p>
  
  <p>The problem is that when I examine the collection of posts generated, the linked Userprofile returns the following exception:</p>
  
  <p><strong>new System.Collections.Generic.Mscorlib_CollectionDebugView(posts).Items[0].Userprofile' threw an exception of type 'System.ObjectDisposedException'</strong></p>
  
  <p>I've tried disabling the DeferredLoadingEnabled attribute in the datacontext and using DataLoadOptions:</p>
  
  <pre><code>var dlo = new DataLoadOptions();
  dlo.LoadWith&lt;Post&gt;(p =&gt; p.Userprofile);
  db.LoadOptions = dlo;
  </code></pre>
  
  <p>Which resulted in the same exception;</p>
  
  <p><a href="https://i.stack.imgur.com/pSwHg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pSwHg.jpg" alt="enter image description here"></a>
  Is there anything else I should be looking at?</p>
  
  <p>Thanks!</p>