From 5ca7f8bdda1c43fdc5a9e3a76c2cbb9ccb56ec14 Mon Sep 17 00:00:00 2001 From: Mihail Date: Mon, 16 Mar 2015 00:18:41 +0300 Subject: [PATCH 1/2] Add Select Extension --- src/PagedList/PagedList.cs | 157 ++++++++++++++++----------- src/PagedList/PagedListExtensions.cs | 15 +++ 2 files changed, 107 insertions(+), 65 deletions(-) diff --git a/src/PagedList/PagedList.cs b/src/PagedList/PagedList.cs index e0e6fd1..be698df 100644 --- a/src/PagedList/PagedList.cs +++ b/src/PagedList/PagedList.cs @@ -1,74 +1,101 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; +using System.Threading; namespace PagedList { - /// - /// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from. - /// - /// - /// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from. - /// - /// The type of object the collection should contain. - /// - /// - /// - /// - [Serializable] - public class PagedList : BasePagedList - { - /// - /// Initializes a new instance of the class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index. - /// - /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. - /// The one-based index of the subset of objects to be contained by this instance. - /// The maximum size of any individual subset. - /// The specified index cannot be less than zero. - /// The specified page size cannot be less than one. - public PagedList(IQueryable superset, int pageNumber, int pageSize) - { - if (pageNumber < 1) - throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1."); - if (pageSize < 1) - throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1."); + /// + /// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from. + /// + /// + /// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from. + /// + /// The type of object the collection should contain. + /// + /// + /// + /// + [Serializable] + public class PagedList : BasePagedList + { + /// + /// Initializes a new instance of the class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index. + /// + /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// The specified index cannot be less than zero. + /// The specified page size cannot be less than one. + public PagedList(IQueryable superset, int pageNumber, int pageSize) + { + if (pageNumber < 1) + throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1."); + if (pageSize < 1) + throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1."); - // set source to blank list if superset is null to prevent exceptions - TotalItemCount = superset == null ? 0 : superset.Count(); - PageSize = pageSize; - PageNumber = pageNumber; - PageCount = TotalItemCount > 0 - ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) - : 0; - HasPreviousPage = PageNumber > 1; - HasNextPage = PageNumber < PageCount; - IsFirstPage = PageNumber == 1; - IsLastPage = PageNumber >= PageCount; - FirstItemOnPage = (PageNumber - 1) * PageSize + 1; - var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1; - LastItemOnPage = numberOfLastItemOnPage > TotalItemCount - ? TotalItemCount - : numberOfLastItemOnPage; + // set source to blank list if superset is null to prevent exceptions + TotalItemCount = superset == null ? 0 : superset.Count(); + PageSize = pageSize; + PageNumber = pageNumber; + PageCount = TotalItemCount > 0 + ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) + : 0; + HasPreviousPage = PageNumber > 1; + HasNextPage = PageNumber < PageCount; + IsFirstPage = PageNumber == 1; + IsLastPage = PageNumber >= PageCount; + FirstItemOnPage = (PageNumber - 1) * PageSize + 1; + var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1; + LastItemOnPage = numberOfLastItemOnPage > TotalItemCount + ? TotalItemCount + : numberOfLastItemOnPage; - // add items to internal list - if (superset != null && TotalItemCount > 0) - Subset.AddRange(pageNumber == 1 - ? superset.Skip(0).Take(pageSize).ToList() - : superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() - ); - } - - /// - /// Initializes a new instance of the class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index. - /// - /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. - /// The one-based index of the subset of objects to be contained by this instance. - /// The maximum size of any individual subset. - /// The specified index cannot be less than zero. - /// The specified page size cannot be less than one. - public PagedList(IEnumerable superset, int pageNumber, int pageSize) - : this(superset.AsQueryable(), pageNumber, pageSize) - { - } - } + // add items to internal list + if (superset != null && TotalItemCount > 0) + Subset.AddRange(pageNumber == 1 + ? superset.Skip(0).Take(pageSize).ToList() + : superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() + ); + } + + + /// + /// Initializes a new instance of the class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index. + /// + /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// The specified index cannot be less than zero. + /// The specified page size cannot be less than one. + public PagedList(IEnumerable superset, int pageNumber, int pageSize) + : this(superset.AsQueryable(), pageNumber, pageSize) + { + } + + /// + /// For Clone PagedList + /// + /// Source PagedList + /// Source collection + public PagedList(PagedListMetaData pagedListMetaData, IEnumerable superset) + { + + // set source to blank list if superset is null to prevent exceptions + TotalItemCount = pagedListMetaData.TotalItemCount; + PageSize = pagedListMetaData.PageSize; + PageNumber = pagedListMetaData.PageNumber; + PageCount = pagedListMetaData.PageCount; + HasPreviousPage = pagedListMetaData.HasPreviousPage; + HasNextPage = pagedListMetaData.HasNextPage; + IsFirstPage = pagedListMetaData.IsFirstPage; + IsLastPage = pagedListMetaData.IsLastPage; + FirstItemOnPage = pagedListMetaData.FirstItemOnPage; + LastItemOnPage = pagedListMetaData.LastItemOnPage; + + Subset.AddRange(superset); + } + + } } \ No newline at end of file diff --git a/src/PagedList/PagedListExtensions.cs b/src/PagedList/PagedListExtensions.cs index 912d410..00e6dbd 100644 --- a/src/PagedList/PagedListExtensions.cs +++ b/src/PagedList/PagedListExtensions.cs @@ -70,5 +70,20 @@ public static IEnumerable> Partition(this IEnumerable super yield return superset.Skip(pageSize * i).Take(pageSize); } } + + /// + /// Cast to Custom Type + /// + /// Source + /// Selector + /// Input Type + /// Result Type + /// New PagedList + public static IPagedList Select(this PagedList source, Func selector) + { + var subset = ((IEnumerable)source).Select(selector); + return new PagedList(source, subset); + } + } } \ No newline at end of file From 497cabbdfec58102c0e269ab15683fb16475464b Mon Sep 17 00:00:00 2001 From: Mikhail Halai Date: Mon, 16 Mar 2015 01:59:48 +0300 Subject: [PATCH 2/2] Add PagedListForEntityFramework (add OrderBy where skip) --- src/PagedList/PagedList.cs | 53 ++++++- src/PagedList/PagedList.csproj | 1 + src/PagedList/PagedListExtensions.cs | 147 ++++++++++-------- src/PagedList/PagedListForEntityFramework .cs | 86 ++++++++++ 4 files changed, 220 insertions(+), 67 deletions(-) create mode 100644 src/PagedList/PagedListForEntityFramework .cs diff --git a/src/PagedList/PagedList.cs b/src/PagedList/PagedList.cs index be698df..1d1ed96 100644 --- a/src/PagedList/PagedList.cs +++ b/src/PagedList/PagedList.cs @@ -20,6 +20,16 @@ namespace PagedList [Serializable] public class PagedList : BasePagedList { + + + /// + /// + /// + protected PagedList() + { + } + + /// /// Initializes a new instance of the class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index. /// @@ -55,7 +65,46 @@ public PagedList(IQueryable superset, int pageNumber, int pageSize) // add items to internal list if (superset != null && TotalItemCount > 0) Subset.AddRange(pageNumber == 1 - ? superset.Skip(0).Take(pageSize).ToList() + ? superset.Take(pageSize).ToList() + : superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() + ); + } + + /// + /// + /// + /// + /// + /// + /// + /// + public PagedList(IQueryable superset, Expression> keySelector, int pageNumber, int pageSize) + { + if (pageNumber < 1) + throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1."); + if (pageSize < 1) + throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1."); + + // set source to blank list if superset is null to prevent exceptions + TotalItemCount = superset == null ? 0 : superset.Count(); + PageSize = pageSize; + PageNumber = pageNumber; + PageCount = TotalItemCount > 0 + ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) + : 0; + HasPreviousPage = PageNumber > 1; + HasNextPage = PageNumber < PageCount; + IsFirstPage = PageNumber == 1; + IsLastPage = PageNumber >= PageCount; + FirstItemOnPage = (PageNumber - 1) * PageSize + 1; + var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1; + LastItemOnPage = numberOfLastItemOnPage > TotalItemCount + ? TotalItemCount + : numberOfLastItemOnPage; + + // add items to internal list + if (superset != null && TotalItemCount > 0) + Subset.AddRange(pageNumber == 1 ? superset.Take(pageSize).ToList() : superset.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() ); } @@ -81,8 +130,6 @@ public PagedList(IEnumerable superset, int pageNumber, int pageSize) /// Source collection public PagedList(PagedListMetaData pagedListMetaData, IEnumerable superset) { - - // set source to blank list if superset is null to prevent exceptions TotalItemCount = pagedListMetaData.TotalItemCount; PageSize = pagedListMetaData.PageSize; PageNumber = pagedListMetaData.PageNumber; diff --git a/src/PagedList/PagedList.csproj b/src/PagedList/PagedList.csproj index 171669f..7656060 100644 --- a/src/PagedList/PagedList.csproj +++ b/src/PagedList/PagedList.csproj @@ -67,6 +67,7 @@ + diff --git a/src/PagedList/PagedListExtensions.cs b/src/PagedList/PagedListExtensions.cs index 00e6dbd..e0373ee 100644 --- a/src/PagedList/PagedListExtensions.cs +++ b/src/PagedList/PagedListExtensions.cs @@ -1,75 +1,78 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; namespace PagedList { - /// - /// Container for extension methods designed to simplify the creation of instances of . - /// - public static class PagedListExtensions - { - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. - /// The one-based index of the subset of objects to be contained by this instance. - /// The maximum size of any individual subset. - /// A subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. - /// - public static IPagedList ToPagedList(this IEnumerable superset, int pageNumber, int pageSize) - { - return new PagedList(superset, pageNumber, pageSize); - } + /// + /// Container for extension methods designed to simplify the creation of instances of . + /// + public static class PagedListExtensions + { + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// A subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. + /// + public static IPagedList ToPagedList(this IEnumerable superset, int pageNumber, int pageSize) + { + return new PagedList(superset, pageNumber, pageSize); + } + + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// Type For Compare + /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// A subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. + /// + public static IPagedList ToPagedList(this IQueryable superset, int pageNumber, int pageSize) + { + return new PagedList(superset, pageNumber, pageSize); + } - /// - /// Creates a subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. - /// - /// The type of object the collection should contain. - /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. - /// The one-based index of the subset of objects to be contained by this instance. - /// The maximum size of any individual subset. - /// A subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. - /// - public static IPagedList ToPagedList(this IQueryable superset, int pageNumber, int pageSize) - { - return new PagedList(superset, pageNumber, pageSize); - } - /// - /// Splits a collection of objects into n pages with an (for example, if I have a list of 45 shoes and say 'shoes.Split(5)' I will now have 4 pages of 10 shoes and 1 page of 5 shoes. - /// - /// The type of object the collection should contain. - /// The collection of objects to be divided into subsets. - /// The number of pages this collection should be split into. - /// A subset of this collection of objects, split into n pages. - public static IEnumerable> Split(this IEnumerable superset, int numberOfPages) - { - return superset - .Select((item, index) => new { index, item }) - .GroupBy(x => x.index % numberOfPages) - .Select(x => x.Select(y => y.item)); - } + /// + /// Splits a collection of objects into n pages with an (for example, if I have a list of 45 shoes and say 'shoes.Split(5)' I will now have 4 pages of 10 shoes and 1 page of 5 shoes. + /// + /// The type of object the collection should contain. + /// The collection of objects to be divided into subsets. + /// The number of pages this collection should be split into. + /// A subset of this collection of objects, split into n pages. + public static IEnumerable> Split(this IEnumerable superset, int numberOfPages) + { + return superset + .Select((item, index) => new { index, item }) + .GroupBy(x => x.index % numberOfPages) + .Select(x => x.Select(y => y.item)); + } - /// - /// Splits a collection of objects into an unknown number of pages with n items per page (for example, if I have a list of 45 shoes and say 'shoes.Partition(10)' I will now have 4 pages of 10 shoes and 1 page of 5 shoes. - /// - /// The type of object the collection should contain. - /// The collection of objects to be divided into subsets. - /// The maximum number of items each page may contain. - /// A subset of this collection of objects, split into pages of maximum size n. - public static IEnumerable> Partition(this IEnumerable superset, int pageSize) - { - if (superset.Count() < pageSize) - yield return superset; - else - { - var numberOfPages = Math.Ceiling(superset.Count() / (double)pageSize); - for (var i = 0; i < numberOfPages; i++) - yield return superset.Skip(pageSize * i).Take(pageSize); - } - } + /// + /// Splits a collection of objects into an unknown number of pages with n items per page (for example, if I have a list of 45 shoes and say 'shoes.Partition(10)' I will now have 4 pages of 10 shoes and 1 page of 5 shoes. + /// + /// The type of object the collection should contain. + /// The collection of objects to be divided into subsets. + /// The maximum number of items each page may contain. + /// A subset of this collection of objects, split into pages of maximum size n. + public static IEnumerable> Partition(this IEnumerable superset, int pageSize) + { + if (superset.Count() < pageSize) + yield return superset; + else + { + var numberOfPages = Math.Ceiling(superset.Count() / (double)pageSize); + for (var i = 0; i < numberOfPages; i++) + yield return superset.Skip(pageSize * i).Take(pageSize); + } + } /// /// Cast to Custom Type @@ -85,5 +88,21 @@ public static IPagedList Select(this PagedList(source, subset); } - } + /// + /// Creates a subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. + /// + /// The type of object the collection should contain. + /// Type For Compare + /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. + /// Expression for Order + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// A subset of this collection of objects that can be individually accessed by index and containing metadata about the collection of objects the subset was created from. + /// + public static IPagedList ToPagedListForEntityFramework(this IQueryable superset, Expression> keySelector, int pageNumber, int pageSize) + { + return new PagedListForEntityFramework(superset, keySelector, pageNumber, pageSize); + } + + } } \ No newline at end of file diff --git a/src/PagedList/PagedListForEntityFramework .cs b/src/PagedList/PagedListForEntityFramework .cs new file mode 100644 index 0000000..ded1ff9 --- /dev/null +++ b/src/PagedList/PagedListForEntityFramework .cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; + +namespace PagedList +{ + /// + /// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from. + /// + /// + /// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from. + /// + /// The type of object the collection should contain. + /// + /// + /// + /// + /// + [Serializable] + public class PagedListForEntityFramework : PagedList + { + /// + /// Initializes a new instance of the class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index. + /// + /// The collection of objects to be divided into subsets. If the collection implements , it will be treated as such. + /// Expression for Order + /// The one-based index of the subset of objects to be contained by this instance. + /// The maximum size of any individual subset. + /// The specified index cannot be less than zero. + /// The specified page size cannot be less than one. + public PagedListForEntityFramework(IQueryable superset, Expression> keySelector, int pageNumber, int pageSize) + { + if (pageNumber < 1) + throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "PageNumber cannot be below 1."); + if (pageSize < 1) + throw new ArgumentOutOfRangeException("pageSize", pageSize, "PageSize cannot be less than 1."); + + // set source to blank list if superset is null to prevent exceptions + TotalItemCount = superset == null ? 0 : superset.Count(); + PageSize = pageSize; + PageNumber = pageNumber; + PageCount = TotalItemCount > 0 + ? (int)Math.Ceiling(TotalItemCount / (double)PageSize) + : 0; + HasPreviousPage = PageNumber > 1; + HasNextPage = PageNumber < PageCount; + IsFirstPage = PageNumber == 1; + IsLastPage = PageNumber >= PageCount; + FirstItemOnPage = (PageNumber - 1) * PageSize + 1; + var numberOfLastItemOnPage = FirstItemOnPage + PageSize - 1; + LastItemOnPage = numberOfLastItemOnPage > TotalItemCount + ? TotalItemCount + : numberOfLastItemOnPage; + + // add items to internal list + if (superset != null && TotalItemCount > 0) + Subset.AddRange(pageNumber == 1 + ? superset.Take(pageSize).ToList() + : superset.OrderBy(keySelector).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList()); + } + + /// + /// For Clone PagedList + /// + /// Source PagedList + /// Source collection + public PagedListForEntityFramework(PagedListMetaData pagedListMetaData, IEnumerable superset) + { + TotalItemCount = pagedListMetaData.TotalItemCount; + PageSize = pagedListMetaData.PageSize; + PageNumber = pagedListMetaData.PageNumber; + PageCount = pagedListMetaData.PageCount; + HasPreviousPage = pagedListMetaData.HasPreviousPage; + HasNextPage = pagedListMetaData.HasNextPage; + IsFirstPage = pagedListMetaData.IsFirstPage; + IsLastPage = pagedListMetaData.IsLastPage; + FirstItemOnPage = pagedListMetaData.FirstItemOnPage; + LastItemOnPage = pagedListMetaData.LastItemOnPage; + + Subset.AddRange(superset); + } + + } +} \ No newline at end of file