title: How to store results of a LINQ To SQL query in memory
body: <p>I was wondering how I can pull down a portion of a table using LINQ To SQL into C# memory space. Let's say I have a database with thousands of records and a GUI interface that shows one record at a time.</p>
  
  <p>When the user clicks the next / previous record button on the GUI the LINQ To SQL datacontext has to go and get the next record - this takes time.</p>
  
  <p>What I would like to do is get a small block of records, say the last n records and store them in memory so the next and previous buttons react much faster.</p>
  
  <p>What I have tried is create a new field of type <code>Table &lt;vw_RawData&gt;</code> called <code>Table_RawData</code> where vw_RawData is a table in my database and was created by Visual Studio when building the datacontext. Then I tried to fill it (<code>onLoad</code>) using</p>
  
  <pre><code>this._Table_Rawdata = (Table&lt;vw_RawData&gt;) 
  from c 
  in this._Db.vw_RawDatas 
  where c.Date &gt; DateTime.Now.AddMonths(-1) 
  select c;
  </code></pre>
  
  <p>This is compiling OK, but at runtime I get the following error... help??</p>
  
  <blockquote>
    <p>System.InvalidCastException: Unable to cast object of type 'System.Data.Linq.DataQuery<code>1[vw_RawData]' to type 'System.Data.Linq.Table</code>1[vw_RawData]'.</p>
  </blockquote>