The official Fatica Labs Blog! RSS 2.0
# Friday, May 16, 2008

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.
Friday, May 16, 2008 1:43:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback

Comments are closed.
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Felice Pollano
Sign In
Statistics
Total Posts: 67
This Year: 41
This Month: 2
This Week: 0
Comments: 36
This blog visits
Locations of visitors to this page
All Content © 2010, Felice Pollano
DasBlog theme 'Business' created by Christoph De Baene (delarou) and modified by Felice Pollano