/Code/Foundation/PaginatedCollection.cs

https://github.com/DavidMoore/Foundation
C# | 110 lines | 40 code | 15 blank | 55 comment | 0 complexity | d4d2775502f247d15f35d9047b8852c1 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. namespace Foundation
  6. {
  7. /// <summary>
  8. /// Adds pagination information to a collection.
  9. /// </summary>
  10. /// <typeparam name="T"></typeparam>
  11. public class PaginatedCollection<T> : List<T>, IPaginatedCollection<T>
  12. {
  13. const int defaultPageSize = 25;
  14. protected readonly IList<string> pages;
  15. /// <summary>
  16. /// Gets or sets the current page number.
  17. /// </summary>
  18. /// <value>The page.</value>
  19. public int Page { get; private set; }
  20. /// <summary>
  21. /// Gets or sets the number of records to return per page.
  22. /// </summary>
  23. /// <value>The size of the page.</value>
  24. public int PageSize { get; private set; }
  25. /// <summary>
  26. /// Gets the total number of records.
  27. /// </summary>
  28. /// <value>The record count.</value>
  29. public int RecordCount { get; private set; }
  30. /// <summary>
  31. /// Gets the total number of pages.
  32. /// </summary>
  33. /// <value>The page count.</value>
  34. public int PageCount { get; private set; }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="PaginatedCollection&lt;T&gt;"/> class.
  37. /// </summary>
  38. public PaginatedCollection()
  39. {
  40. Page = 1;
  41. PageSize = defaultPageSize;
  42. RecordCount = 0;
  43. PageCount = 1;
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="PaginatedCollection&lt;T&gt;"/> class.
  47. /// </summary>
  48. /// <param name="source">The source.</param>
  49. /// <param name="page">The page.</param>
  50. /// <param name="pageSize">Size of the page.</param>
  51. public PaginatedCollection(IEnumerable<T> source, int page, int pageSize) : this(source, page, pageSize, source.Count()){}
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="PaginatedCollection&lt;T&gt;"/> class.
  54. /// </summary>
  55. /// <param name="source">The source.</param>
  56. /// <param name="page">The page.</param>
  57. /// <param name="pageSize">Size of the page.</param>
  58. /// <param name="recordCount">The record count.</param>
  59. public PaginatedCollection(IEnumerable<T> source, int page, int pageSize, int recordCount)
  60. {
  61. Page = page;
  62. PageSize = pageSize;
  63. RecordCount = recordCount;
  64. PageCount = (int)Math.Ceiling(RecordCount / (double)PageSize);
  65. // Don't skip if there's not enough records. This can happen if we've already been passed
  66. // the page data rather than the whole list
  67. var skip = source.Count() <= pageSize ? 0 : (Page - 1) * PageSize;
  68. // Default page names
  69. pages = Enumerable.Range(1, PageCount).ToList().ConvertAll(input => input.ToString(CultureInfo.CurrentCulture));
  70. AddRange(source.Skip(skip).Take(pageSize));
  71. }
  72. /// <summary>
  73. /// Gets a value indicating whether there is a page of results preceding this one.
  74. /// </summary>
  75. /// <value>
  76. /// <c>true</c> if this instance has a previous page; otherwise, <c>false</c>.
  77. /// </value>
  78. public bool HasPreviousPage { get { return Page > 1; } }
  79. /// <summary>
  80. /// Gets a value indicating whether there is another page of results following this one.
  81. /// </summary>
  82. /// <value>
  83. /// <c>true</c> if this instance has a next page; otherwise, <c>false</c>.
  84. /// </value>
  85. public bool HasNextPage { get { return (Page < PageCount); } }
  86. /// <summary>
  87. /// A collection of names for the pages. Usually this defaults to the page numbers but can
  88. /// be changed depending on the pagination strategy, i.e. alphabetical names for alphabetical pagination
  89. /// </summary>
  90. public IEnumerable<string> PageNames
  91. {
  92. get { return pages; }
  93. }
  94. }
  95. }