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


All comments require the approval of the site owner before being displayed.
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, strike, strong) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
My Stack Overflow
Contacts

Send mail to the author(s) E-mail

Tags
profile for Felice Pollano at Stack Overflow, Q&A for professional and enthusiast programmers
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 2012
Felice Pollano
Sign In
Statistics
Total Posts: 143
This Year: 3
This Month: 0
This Week: 0
Comments: 105
This blog visits
All Content © 2012, Felice Pollano
DasBlog theme 'Business' created by Christoph De Baene (delarou) and modified by Felice Pollano