Up Display code samples
DevForce 2010 Resource Center » Code samples » Display code samples » Code sample: Use a paged collection (Silverlight)

Code sample: Use a paged collection (Silverlight)

Last modified on August 15, 2012 17:20

This sample illustrates how to use the DevForce EntityQueryPagedCollectionView (EqPCV) with the Silverlight DataPager control.


Problem

You'd like to use a paged collection in your Silverlight application.  It needs to execute queries asychronously, and handle sorting, grouping and filtering.

Solution

This sample illustrates how to use the DevForce EntityQueryPagedCollectionView (EqPCV) with the Silverlight DataPager control.  The EntityQueryPagedCollectionView is an IPagedCollectionView implementation which provides paging capabilities using asynchronous queries.  

The EntityQueryPagedCollectionView is similar in functionality to the ObjectDataSource (ODS), but does not include any XAML markup and can be used in view models in MVVM architectures or in code behind.  Like the ODS, the EqPCV supports grouping, sorting and filtering.

The EntityQueryPagedCollectionView is currently available in Silverlight only.

Deconstructing the sample

MainPage.xaml

In the sample a single page, MainPage, contains the Silverlight DataGrid and DataPager controls.  Both the grid's ItemSource and the DataPager's Source properties are bound to the EqPCV via the MainPageViewModel.

Constructing an EntityQueryPagedCollectionView

C#
var query = _entityManager.Customers;

EntityQueryPagedCollectionView pcv = new EntityQueryPagedCollectionView(
         query,                  // Source of data
        _dataPager.PageSize,    // Page size
        _dataPager.PageSize,    // Load size - no lookahead caching here
        true,                   // Whether to defer the load
        false);                 // Whether to add primary key to sort columns

pcv.SortDescriptions.Add(new SortDescription("CompanyName", ListSortDirection.Ascending));
pcv.Refresh();

The EqPCV takes an EntityQuery<T> in its constructor - this will serve as the source for data in the view. The query can be of any complexity, but if it contains an OrderBy method any sort columns selected by the user in the datagrid will be added as ThenBy methods. 

You cannot use the EqPCV with a StoredProcQuery or PassthruEsqlQuery because paged queries to the data source cannot be constructed with these query types. With these queries, use the standard Silverlight PagedCollectionView to provide paging of the returned data.

Don't execute the query: the EqPCV is responsible for executing the query as each page is requested. The EqPCV uses the Take and Skip LINQ methods to page through the data. A DataSourceOnly query strategy is used to first load each page, while subsequent requests for the page are executed against cache. If you require that all page requests be serviced only from the DataSource or only from cache, then set the appropriate query strategy on the query provided to the EqPCV. Note that if you do use a CacheOnly strategy you should ensure that the data is already cached.

The page and load sizes are also provided to the EqPCV constructor. The PageSize indicates how many items should be displayed for each page, while the LoadSize indicates how many items should be loaded on each page request.  Set the LoadSize to a multiple of the PageSize value to perform “look ahead” caching of pages.

The defer load parameter to the constructor indicates whether the query should be immediately executed within the constructor. If you defer the load to later, be sure to call the Refresh() method to load data.

The constructor also accepts a flag indicating whether the data should be sorted by primary key (the EntityKey of the entity). If true, the EqPCV will add the primary key to the SortDescriptions.

Prerequisites

Created by DevForce on July 14, 2010 18:39

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade