here are some applications where we need to display large sets of data. In order to display this large sets of data we usually follow the approach of Data paging. In asp.net there are data bound controls like Gridview, listview etc that can facilitate paging. The problem with this databound controls is that they perform paging on complete datasets. The asp.net databound controls brings the complete datasets and then perform paging at the application server side. This can led to network overload and server memory consuming every time user requests new data page. Paging done at the database level is the best technique to optimize performance. In order to achieve this we have to build the appropriate query that will return only the required page result items. In Entity Framework , we can do this by implementing an IQueryable (http://msdn.microsoft.com/en-us/library/bb351562.aspx ) interface and building the required EF queries to get the paged result items from the database. |
The following is an implementation of a simple extension method to offer paging functionality to simple Enitity framework or Linq to Sql query providers. |
/// /// Pages the specified query /// /// Generic Type Object /// The type of the result. /// The Object query where paging needs to be applied. /// The page number. /// Size of the page. /// The order by property. /// if set to true [is ascending order]. /// The total rows count. /// private static IQueryable PagedResult(IQueryable query, int pageNum, int pageSize, Expression orderByProperty, bool isAscendingOrder, out int rowsCount) { if (pageSize 0 else set to first page if (rowsCount article.PublishedDate, false, out totalArticles);
Accordingly this method can be invoked by passing the page number for other pages.