Felice Pollano Blog

The Official Fatica Labs Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

    Disclaimer

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

    © Copyright 2008

    How To: Bind a Linq to SQL query results to a VirtualMode ListView

    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.

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    Tags:
    Posted by Felice on Friday, May 16, 2008 2:43 PM
    Permalink | Comments (0) | Post RSSRSS comment feed

    Related posts

    Add comment


    (Will show your Gravatar icon)  

      Country flag




    Live preview

    Friday, November 21, 2008 7:22 PM

    Gravatar