title: Returning IQueryable from within a using block. Need better design
body: <p>I have created a PhoneBook style application; on my phonebook object I have a local member <code>_site</code> which is used as a filter, since there are approximately 1000 phone numbers, split across 12 sites within my organisation. Only one site will be retrieved at a time using this method.</p>
  
  <p>This was my original method. The GUI has several methods for reordering the data, so I left it as an IQueryable because I would like to defer SQL to allow for filtering to be done on the SQL server rather than on the client PC.</p>
  
  <p><strong>Works</strong></p>
  
  <pre><code>public IQueryable&lt;PhoneNumber&gt; GetPhoneDirectory()
  {
      PhoneBookDataContext db = new PhoneBookDataContext())
      return db.PhoneNumbers.Where(d =&gt; d.Site == _site);
  }
  </code></pre>
  
  <p>However, I am also trying to keep to 'best practise' in terms of <code>using</code> statements.</p>
  
  <p><strong>Doesn't Work</strong></p>
  
  <pre><code>public IQueryable&lt;PhoneNumber&gt; GetPhoneDirectory()
  {
      using (PhoneBookDataContext db = new PhoneBookDataContext())
      {
          return db.PhoneNumbers.Where(d =&gt; d.Site == _site);
      }
  }
  </code></pre>
  
  <p>Now as pointed out by @justanotheruseryoumay, this will cause an exception because the datacontext is disposed by the time the objects are accessed.</p>
  
  <p>I guess what I am asking is, how can I make sure my data context is disposed nicely, when I cannot use a 'using' statement and don't strictly know when the context is done with.</p>