Generic + extension method to create a simple paging class (C #)

Source: Internet
Author: User

Create a new class named PagedList and inherit from List <T>

Code
1 public class PagedList <T>: List <T>
2 {
3 /// <summary>
4 // page number
5 /// </summary>
6 public int PageIndex {get; set ;}
7
8 /// <summary>
9 // page size
10 /// </summary>
11 public int PageSize {get; set ;}
12
13 /// <summary>
14 // total number of elements
15 /// </summary>
16 public int TotalCount {get; set ;}
17
18 /// <summary>
19 // page number
20 /// </summary>
21 public int PageCount {get; set ;}
22
23 /// <summary>
24 // Constructor
25 /// </summary>
26 /// <param name = "list"> linked list </param>
27 /// <param name = "intPageIndex"> NO. </param>
28 /// <param name = "intPageSize"> size </param>
29 public PagedList (List <T> list, int intPageIndex, int intPageSize)
30 {
31 PageIndex = intPageIndex;
32 PageSize = intPageSize;
33
34 GetPagedList (list );
35}
36
37 // <summary>
38 // convert to pagination
39 /// </summary>
40 /// <param name = "list"> linked list </param>
41 private void GetPagedList (List <T> list)
42 {
43 int intStart = (PageIndex-1) * PageSize;
44
45 for (int I = intStart; I <intStart + PageSize & I <list. Count; I ++)
46 {
47 This. Add (list [I]);
48}
49
50 totalcount = List. count;
51 pagecount = totalcount/pagesize + 1;
52}
53}

Create a new class named PagedListExpansion.

Code
Public static class pagedlistexpansion
{
Public static PagedList <T> ToPagedList <T> (this List <T> list, int intPageIndex, int intPageSize)
{
Return new PagedList <T> (list, intPageIndex, intPageSize );
}
}

This simple paging class is complete.

Call:

Code
List <int> list = new List <int> ();
PagedList <int> page = list. ToPagedList <int> (1, 10 );

Such paging classes can be used for all types of linked lists. This is just an enlightening Demo. Please point out any errors. There are better ways to solve them. Remember to propose them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.