The ListView VirtualMode allow very efficient data display of very large resultset. We can mix this ability with Linq Skip(n) and Take(m) to bind a IQueryable<T> to a virtual list view. To efficently avoid to query the underlaying database, we have to cache in some way the results with some paging strategy.
Below the solution I used:
public class LinqPager<T>
{
Dictionary<int, List<T>> pages = new Dictionary<int,List<T>>();
IQueryable<T> queryable;
int pageSize;public LinqPager(IQueryable<T> queryable,int pageSize)
{
this.pageSize = pageSize;
this.queryable = queryable;
}
public void InvalidatePages()
{
pages.Clear();
}
public T this[int index]
{
get {int page, offset;
page = index / pageSize;
offset = index % pageSize;
if (!pages.ContainsKey(page))
{
pages[page] = new List<T>(pageSize);int start = pageSize * page;
pages[page].AddRange(queryable.Skip<T>(start).Take<T>(pageSize));
}
return pages[page][offset];
}
}
}
The class above has a constructor taking an IQueryable<T> and a page size. It will query the DB with chunks pageSize long, and provide an indexer to randomly access the result. This accessor can be used to fill the item on the RetrieveVirtualItem event.