title: Efficient Custom Paging in ASP.NET 2.0 while sorting
body: <p>I've got an web app in ASP.NET 2.0 in which I need to do paging.  My method of data access is to pull a <code>DataSet</code> out of a database call, then convert that to a <code>List&lt;Foo&gt;</code> (where Foo is my type I'm pulling out of the DB) and bind my <code>GridView</code> to it.  My reason for this is that I didn't want to be having to use string indexers on DataTables all through my application, and that I could separate the display logic from the database by implementing display logic as properties on my classes.  This also means I'm doing sorting in .NET instead of SQL.</p>
  
  <p>To implement paging, then, I need to pull all <code>Foo</code> out of the database, sort the list, then take what I want out of the full list to display:</p>
  
  <pre><code>List&lt;Foo&gt; myFoo = MyDB.GetFoos();
  myFoo.Sort(new Foo.FooComparer());
  List&lt;Foo&gt; toDisplay = new List&lt;Foo&gt;();
  for (int i = pageIndex * pageSize; i &lt; (pageIndex + 1) * pageSize &amp;&amp; i &lt; myFoo.Count; i++)
  {
    toDisplay.Add(myFoo[i]);
  }
  //bind grid
  </code></pre>
  
  <p>With enough elements, this becomes a source of delay; on my development machine connecting to the test database, it takes almost 0.5 seconds to bind one grid on the screen when pulling 5000 records from the DB.</p>
  
  <p>To solve this problem, am I going to have to move all my display logic to SQL so the sorting and paging can take place there, or is there a better way?</p>
  
  <p>Also, does Linq to SQL solve this?  If I'm sorting by a custom property implemented in my .NET class and then using <code>.Skip(pageIndex * pageSize).Take(pageSize)</code>, will it convert that to SQL as noted in <a href="https://stackoverflow.com/questions/548475/efficient-way-to-implement-paging">this question</a>?</p>