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 DataSet
out of a database call, then convert that to a List<Foo>
(where Foo is my type I'm pulling out of the DB) and bind my GridView
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.
To implement paging, then, I need to pull all Foo
out of the database, sort the list, then take what I want out of the full list to display:
List<Foo> myFoo = MyDB.GetFoos();
myFoo.Sort(new Foo.FooComparer());
List<Foo> toDisplay = new List<Foo>();
for (int i = pageIndex * pageSize; i < (pageIndex + 1) * pageSize && i < myFoo.Count; i++)
{
toDisplay.Add(myFoo[i]);
}
//bind grid
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.
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?
Also, does Linq to SQL solve this? If I'm sorting by a custom property implemented in my .NET class and then using .Skip(pageIndex * pageSize).Take(pageSize)
, will it convert that to SQL as noted in this question?