From 22806c7d16bdf8cccd314e1164af81a2e4fcd50c Mon Sep 17 00:00:00 2001 From: tmacharia Date: Tue, 16 Mar 2021 03:32:51 +0300 Subject: [PATCH 1/7] drops net47 & retargets to net48 --- src/Paginator.csproj | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Paginator.csproj b/src/Paginator.csproj index 1769dce..42546fd 100644 --- a/src/Paginator.csproj +++ b/src/Paginator.csproj @@ -5,7 +5,8 @@ netstandard2.0; netcoreapp3.0;netcoreapp2.0;netcoreapp2.1; - net47 + + net48 false true @@ -18,19 +19,18 @@ https://github.com/tmacharia/linq-paginator linq, pagination, list paginator, paging, list, array, collection For queries that return a lot of data, a need emerges to consume the results in chunks rather than the entire set. Consuming all results at once can be costly in terms of network traffic thus slowing down your application. - Linq Paginator allows you to run your queries and return your data in form of pages. A page contains a set number of items to return per page e.g 20. If for example you have 100 records, it will return the following object: + Linq Paginator allows you to run your queries and return your data in form of pages. A page contains a set number of items to return per page e.g 20. If for example you have 100 records, it will return the following object: - Page: 1, - TotalPages: 5, - ItemsPerPage: 20, - TotalItems: 100, - - List (collection of the first 20 records) + - Items (collection of the first 20 records) LinqPaginator 1.2.6 en - Fixes bug/issue with pagination result: - -> TotalPages is always zero + Fixes bug/issue casting. git @@ -43,7 +43,7 @@ --> - + From 3610b9eacab9a4a4db302942060bbb28b82651ee Mon Sep 17 00:00:00 2001 From: tmacharia Date: Tue, 16 Mar 2021 03:35:08 +0300 Subject: [PATCH 2/7] changes ICollection to IList --- src/PagedResult.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PagedResult.cs b/src/PagedResult.cs index 644000d..9199284 100644 --- a/src/PagedResult.cs +++ b/src/PagedResult.cs @@ -22,7 +22,7 @@ public class PagedResult /// public PagedResult() { - Items = new HashSet(); + Items = new List(); } /// /// Current page in pagination @@ -45,7 +45,7 @@ public PagedResult() /// /// Collection containing items in the current page. /// - public ICollection Items { get; set; } + public IList Items { get; set; } /// /// Calculates & returns the hashcode of the current object. From bd4d6ca5b258cb890cc8b41a9bf8195d8eae4427 Mon Sep 17 00:00:00 2001 From: tmacharia Date: Tue, 16 Mar 2021 04:29:54 +0300 Subject: [PATCH 3/7] enable skipping count --- .editorconfig | 4 +++ Paginator.sln | 3 +- src/Paginator.cs | 70 ++++++++++++++++++++++++++++++++++++-------- src/Paginator.csproj | 1 + 4 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a05b287 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# IDE0011: Add braces +csharp_prefer_braces = when_multiline diff --git a/Paginator.sln b/Paginator.sln index 5c9b300..c051370 100644 --- a/Paginator.sln +++ b/Paginator.sln @@ -5,10 +5,11 @@ VisualStudioVersion = 16.0.29006.145 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paginator", "src\Paginator.csproj", "{372B4525-5399-4792-BBC7-33BB8897F191}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "tests\TestCases.csproj", "{48A1CCEB-85F0-4091-8B9F-154D61AF1D5A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestCases", "tests\TestCases.csproj", "{48A1CCEB-85F0-4091-8B9F-154D61AF1D5A}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5A9AA53E-5B35-419C-BBCA-C42DD98FBE71}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig .gitattributes = .gitattributes .gitignore = .gitignore _config.yml = _config.yml diff --git a/src/Paginator.cs b/src/Paginator.cs index 5c3a690..a22b3ec 100644 --- a/src/Paginator.cs +++ b/src/Paginator.cs @@ -18,6 +18,7 @@ public static class Paginator /// Number of items to return per page. /// private const int _perpage = 10; + private const bool _skipCount = false; #region .Paginate Methods /// @@ -35,12 +36,13 @@ public static PagedResult Paginate(this IEnumerable enumerable) /// Type of entity in collection. /// The page to retrive /// Number of items per page. + /// Whether to avoid running count query & only get required items. /// The current collection. /// . - public static PagedResult Paginate(this IEnumerable enumerable, - int page=_page, int perpage=_perpage) - where T : class - => enumerable.ProcessPagination(page, perpage); + public static PagedResult Paginate(this IEnumerable enumerable, + int page = _page, int perpage = _perpage, bool skipCount = _skipCount) + where T : class + => enumerable.ProcessPagination(page, perpage, skipCount); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// @@ -150,6 +152,7 @@ public static PagedResult Page(this IEnumerable enumerable, FuncType of entity in collection. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Paged(this IEnumerable enumerable) where T : class => enumerable.ProcessPagination(_page, _perpage); @@ -161,6 +164,7 @@ public static PagedResult Paged(this IEnumerable enumerable) /// The page to retrieve. /// Number of items per page. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Paged(this IEnumerable enumerable, int page = _page, int perpage = _perpage) where T : class @@ -172,6 +176,7 @@ public static PagedResult Paged(this IEnumerable enumerable) /// The current collection. /// A filter to apply to the collection before pagination. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Paged(this IEnumerable enumerable, Func predicate) where T : class => enumerable.ProcessPagination(predicate); @@ -184,6 +189,7 @@ public static PagedResult Paged(this IEnumerable enumerable, FuncThe page to retrieve. /// Number of items per page. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Paged(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage) where T : class @@ -199,6 +205,7 @@ public static PagedResult Paged(this IEnumerable enumerable, FuncProperty to order by /// Ordering type e.g asc or desc /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Paged(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage, string orderBy = null, string order = "asc") where T : class @@ -212,6 +219,7 @@ public static PagedResult Paged(this IEnumerable enumerable, FuncType of entity in collection. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPages(this IEnumerable enumerable) where T : class => enumerable.ProcessPagination(_page, _perpage); @@ -223,6 +231,7 @@ public static PagedResult ToPages(this IEnumerable enumerable) /// The page to retrieve. /// Number of items per page. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPages(this IEnumerable enumerable, int page = _page, int perpage = _perpage) where T : class @@ -234,6 +243,7 @@ public static PagedResult ToPages(this IEnumerable enumerable) /// The current collection. /// A filter to apply to the collection before pagination. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPages(this IEnumerable enumerable, Func predicate) where T : class => enumerable.ProcessPagination(predicate); @@ -246,6 +256,7 @@ public static PagedResult ToPages(this IEnumerable enumerable, FuncThe page to retrieve. /// Number of items per page. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPages(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage) where T : class @@ -261,6 +272,7 @@ public static PagedResult ToPages(this IEnumerable enumerable, FuncProperty to order by /// Ordering type e.g asc or desc /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPages(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage, string orderBy = null, string order = "asc") where T : class @@ -274,6 +286,7 @@ public static PagedResult ToPages(this IEnumerable enumerable, FuncType of entity in collection. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPaginate(this IEnumerable enumerable) where T : class => enumerable.ProcessPagination(_page, _perpage); @@ -285,6 +298,7 @@ public static PagedResult ToPaginate(this IEnumerable enumerable) /// The page to retrieve. /// Number of items per page. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPaginate(this IEnumerable enumerable, int page = _page, int perpage = _perpage) where T : class @@ -296,6 +310,7 @@ public static PagedResult ToPaginate(this IEnumerable enumerable) /// The current collection. /// A filter to apply to the collection before pagination. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate) where T : class => enumerable.ProcessPagination(predicate); @@ -308,6 +323,7 @@ public static PagedResult ToPaginate(this IEnumerable enumerable, Func< /// The page to retrieve. /// Number of items per page. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage) where T : class @@ -323,6 +339,7 @@ public static PagedResult ToPaginate(this IEnumerable enumerable, Func< /// Property to order by /// Ordering type e.g asc or desc /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage, string orderBy = null, string order = "asc") where T : class @@ -336,6 +353,7 @@ public static PagedResult ToPaginate(this IEnumerable enumerable, Func< /// Type of entity in collection. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Page(this IQueryable entities) where TEntity : class => entities.ProcessPagination(_page, _perpage); @@ -347,6 +365,7 @@ public static PagedResult Page(this IQueryable entiti /// Number of items per page. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Page(this IQueryable entities, int page = _page, int perpage = _perpage) where TEntity : class @@ -357,6 +376,7 @@ public static PagedResult Page(this IQueryable entiti /// Type of entity in collection. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Paged(this IQueryable entities) where TEntity : class => entities.ProcessPagination(_page, _perpage); @@ -368,6 +388,7 @@ public static PagedResult Paged(this IQueryable entit /// Number of items per page. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult Paged(this IQueryable entities, int page = _page, int perpage = _perpage) where TEntity : class @@ -378,6 +399,7 @@ public static PagedResult Paged(this IQueryable entit /// Type of entity in collection. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPages(this IQueryable entities) where TEntity : class => entities.ProcessPagination(_page, _perpage); @@ -389,6 +411,7 @@ public static PagedResult ToPages(this IQueryable ent /// Number of items per page. /// The current collection. /// . + [Obsolete("Use .Paginate instead")] public static PagedResult ToPages(this IQueryable entities, int page = _page, int perpage = _perpage) where TEntity : class @@ -408,18 +431,29 @@ public static PagedResult Paginate(this IQueryable en /// Type of entity in collection. /// The page to retrive /// Number of items per page. + /// Whether to avoid running count query & only get required items. /// The current collection. /// . public static PagedResult Paginate(this IQueryable entities, - int page = _page, int perpage = _perpage) + int page = _page, int perpage = _perpage, bool skipCount = _skipCount) where TEntity : class - => entities.ProcessPagination(page, perpage); + => entities.ProcessPagination(page, perpage, skipCount); #endregion - internal static PagedResult ProcessPagination(this IQueryable entities, - int page = _page, int perpage = _perpage) + internal static PagedResult ProcessPagination(this IQueryable query, + int page = _page, int perpage = _perpage, bool skipCount = _skipCount) { - int total = entities.CountEntities(); + int total = 0; + var list = new List(); + + if (!skipCount) + total = query.CountEntities(); + + if (skipCount || (!skipCount && total > 0)) + list = query.Skip((page - 1) * perpage).Take(perpage).ToList(); + + if (skipCount) + total = list.Count; return new PagedResult() { @@ -427,13 +461,23 @@ public static PagedResult Paginate(this IQueryable en ItemsPerPage = perpage, TotalItems = total, TotalPages = CalculateTotalPages(total, perpage), - Items = entities.Skip((page - 1) * perpage).Take(perpage).ToList() + Items = list }; } internal static PagedResult ProcessPagination(this IEnumerable enumerable, - int page = _page, int perpage = _perpage) + int page = _page, int perpage = _perpage, bool skipCount = _skipCount) { - int total = enumerable.CountItems(); + int total = 0; + var list = new List(); + + if (!skipCount) + total = enumerable.CountItems(); + + if (skipCount || (!skipCount && total > 0)) + list = enumerable.Skip((page - 1) * perpage).Take(perpage).ToList(); + + if (skipCount) + total = list.Count; return new PagedResult() { @@ -441,7 +485,7 @@ public static PagedResult Paginate(this IQueryable en ItemsPerPage = perpage, TotalItems = total, TotalPages = CalculateTotalPages(total, perpage), - Items = enumerable.Skip((page - 1) * perpage).Take(perpage).ToList() + Items = list }; } internal static PagedResult ProcessPagination(this IEnumerable enumerable, diff --git a/src/Paginator.csproj b/src/Paginator.csproj index 42546fd..602fa0c 100644 --- a/src/Paginator.csproj +++ b/src/Paginator.csproj @@ -48,6 +48,7 @@ + True From 038d447d3f3306792d71fc900586141f51226618 Mon Sep 17 00:00:00 2001 From: tmacharia Date: Tue, 16 Mar 2021 05:22:57 +0300 Subject: [PATCH 4/7] order by func instead of prop name --- Tests/IEnumerableTests.cs | 20 ++------ Tests/IQueryableTests.cs | 11 +--- src/Paginator.cs | 103 +++++++++++++++++++++----------------- 3 files changed, 62 insertions(+), 72 deletions(-) diff --git a/Tests/IEnumerableTests.cs b/Tests/IEnumerableTests.cs index 30a0763..cc7b288 100644 --- a/Tests/IEnumerableTests.cs +++ b/Tests/IEnumerableTests.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using Paginator; using System.Linq; -using System; -using Common; namespace Tests { @@ -41,8 +39,8 @@ public void Empty(ICollection Rates) public void FuncTest(ICollection nums) { int perpage = 5; - - var result = nums.Paged(x => x.Matches("0"), 2, perpage); + + var result = nums.Paged(x => x.Equals("0"), 2, perpage, x => x); int pages = GetPages(result.TotalItems, perpage); Assert.AreEqual(2, result.Page); @@ -53,24 +51,12 @@ public void FuncTest(ICollection nums) [Test, TestCaseSource(typeof(Seed), "Pages")] public void OrderByProperty(ICollection Rates) { - var result = Rates.ToPaginate(null, 1, 10, "Value", "desc"); + var result = Rates.ToPaginate(null, 1, 10, x=>x.Value, "desc"); Assert.IsNotNull(result); Assert.AreEqual(1, result.Page); Assert.AreEqual(Rates.Count, result.TotalItems); Assert.Greater(result.Items.First().Value, result.Items.Last().Value); - result.Items.ForEach(x => - { - Console.WriteLine(x.Value); - }); - } - - [Test, TestCaseSource(typeof(Seed), "Pages")] - public void NullRequestTest(ICollection list) - { - var result = list.ToPages(null); - - Assert.AreEqual(1, result.Page); } } } \ No newline at end of file diff --git a/Tests/IQueryableTests.cs b/Tests/IQueryableTests.cs index 5107e7f..3a29793 100644 --- a/Tests/IQueryableTests.cs +++ b/Tests/IQueryableTests.cs @@ -38,21 +38,12 @@ public void Empty(ICollection Rates) public void FuncTest(ICollection Rates) { var result = Rates.AsQueryable() - .Paged(x => x.Value > 9, 2, 2); + .Paged(x => x.Value > 9, 2, 2, x => x.Value); Assert.NotNull(result); Assert.AreEqual(2, result.Page); Assert.AreEqual(2, result.ItemsPerPage); Assert.AreEqual(Rates.Count(x => x.Value > 9), result.TotalItems); } - - [Test, TestCaseSource(typeof(Seed), "Pages")] - public void NullRequestTest(ICollection list) - { - var result = list.AsQueryable() - .ToPages(null); - - Assert.AreEqual(1, result.Page); - } } } \ No newline at end of file diff --git a/src/Paginator.cs b/src/Paginator.cs index a22b3ec..9a4cf24 100644 --- a/src/Paginator.cs +++ b/src/Paginator.cs @@ -1,5 +1,4 @@ -using Common; -using System; +using System; using System.Collections.Generic; using System.Linq; @@ -47,29 +46,32 @@ public static PagedResult Paginate(this IEnumerable enumerable) /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// A filter to apply to the collection before pagination. /// The current collection. /// . - public static PagedResult Paginate(this IEnumerable enumerable, Func predicate) + public static PagedResult Paginate(this IEnumerable enumerable, Func predicate) where T : class - => enumerable.ProcessPagination(predicate); + => enumerable.ProcessPagination2(predicate); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. /// Number of items per page. /// . - public static PagedResult Paginate(this IEnumerable enumerable, Func predicate, + public static PagedResult Paginate(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage) where T : class - => enumerable.ProcessPagination(predicate, page, perpage); + => enumerable.ProcessPagination2(predicate, page, perpage); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. @@ -77,10 +79,10 @@ public static PagedResult Paginate(this IEnumerable enumerable, FuncProperty to order by /// Ordering type e.g asc or desc /// . - public static PagedResult Paginate(this IEnumerable enumerable, Func predicate, - int page = _page, int perpage = _perpage, string orderBy = null, string order = "asc") + public static PagedResult Paginate(this IEnumerable enumerable, Func predicate, + int page = _page, int perpage = _perpage, Func orderBy = null, string order = "asc") where T : class - => enumerable.ProcessPagination(predicate, page, perpage, orderBy, order); + => enumerable.ProcessPagination2(predicate, page, perpage, orderBy, order); #endregion #region .Page Methods @@ -109,29 +111,32 @@ public static PagedResult Page(this IEnumerable enumerable) /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// . - public static PagedResult Page(this IEnumerable enumerable, Func predicate) + public static PagedResult Page(this IEnumerable enumerable, Func predicate) where T : class - => enumerable.ProcessPagination(predicate); + => enumerable.ProcessPagination2(predicate); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. /// Number of items per page. /// . - public static PagedResult Page(this IEnumerable enumerable, Func predicate, + public static PagedResult Page(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage) where T : class - => enumerable.ProcessPagination(predicate, page, perpage); + => enumerable.ProcessPagination2(predicate, page, perpage); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. @@ -139,10 +144,10 @@ public static PagedResult Page(this IEnumerable enumerable, FuncProperty to order by /// Ordering type e.g asc or desc /// . - public static PagedResult Page(this IEnumerable enumerable, Func predicate, - int page = _page, int perpage = _perpage, string orderBy = null, string order = "asc") + public static PagedResult Page(this IEnumerable enumerable, Func predicate, + int page = _page, int perpage = _perpage, Func orderBy = null, string order = "asc") where T : class - => enumerable.ProcessPagination(predicate, page, perpage, orderBy, order); + => enumerable.ProcessPagination2(predicate, page, perpage, orderBy, order); #endregion #region .Paged Methods @@ -173,31 +178,34 @@ public static PagedResult Paged(this IEnumerable enumerable) /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// . [Obsolete("Use .Paginate instead")] - public static PagedResult Paged(this IEnumerable enumerable, Func predicate) + public static PagedResult Paged(this IEnumerable enumerable, Func predicate) where T : class - => enumerable.ProcessPagination(predicate); + => enumerable.ProcessPagination2(predicate); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. /// Number of items per page. /// . [Obsolete("Use .Paginate instead")] - public static PagedResult Paged(this IEnumerable enumerable, Func predicate, + public static PagedResult Paged(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage) where T : class - => enumerable.ProcessPagination(predicate, page, perpage); + => enumerable.ProcessPagination2(predicate, page, perpage); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. @@ -206,10 +214,10 @@ public static PagedResult Paged(this IEnumerable enumerable, FuncOrdering type e.g asc or desc /// . [Obsolete("Use .Paginate instead")] - public static PagedResult Paged(this IEnumerable enumerable, Func predicate, - int page = _page, int perpage = _perpage, string orderBy = null, string order = "asc") + public static PagedResult Paged(this IEnumerable enumerable, Func predicate, + int page = _page, int perpage = _perpage, Func orderBy = null, string order = "asc") where T : class - => enumerable.ProcessPagination(predicate, page, perpage, orderBy, order); + => enumerable.ProcessPagination2(predicate, page, perpage, orderBy, order); #endregion #region .ToPages Methods @@ -240,31 +248,34 @@ public static PagedResult ToPages(this IEnumerable enumerable) /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// . [Obsolete("Use .Paginate instead")] - public static PagedResult ToPages(this IEnumerable enumerable, Func predicate) + public static PagedResult ToPages(this IEnumerable enumerable, Func predicate) where T : class - => enumerable.ProcessPagination(predicate); + => enumerable.ProcessPagination2(predicate); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. /// Number of items per page. /// . [Obsolete("Use .Paginate instead")] - public static PagedResult ToPages(this IEnumerable enumerable, Func predicate, + public static PagedResult ToPages(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage) where T : class - => enumerable.ProcessPagination(predicate, page, perpage); + => enumerable.ProcessPagination2(predicate, page, perpage); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. @@ -273,10 +284,10 @@ public static PagedResult ToPages(this IEnumerable enumerable, FuncOrdering type e.g asc or desc /// . [Obsolete("Use .Paginate instead")] - public static PagedResult ToPages(this IEnumerable enumerable, Func predicate, - int page = _page, int perpage = _perpage, string orderBy = null, string order = "asc") + public static PagedResult ToPages(this IEnumerable enumerable, Func predicate, + int page = _page, int perpage = _perpage, Func orderBy = null, string order = "asc") where T : class - => enumerable.ProcessPagination(predicate, page, perpage, orderBy, order); + => enumerable.ProcessPagination2(predicate, page, perpage, orderBy, order); #endregion #region .ToPaginate Methods @@ -307,31 +318,34 @@ public static PagedResult ToPaginate(this IEnumerable enumerable) /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// . [Obsolete("Use .Paginate instead")] - public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate) + public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate) where T : class - => enumerable.ProcessPagination(predicate); + => enumerable.ProcessPagination2(predicate); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. /// Number of items per page. /// . [Obsolete("Use .Paginate instead")] - public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate, + public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate, int page = _page, int perpage = _perpage) where T : class - => enumerable.ProcessPagination(predicate, page, perpage); + => enumerable.ProcessPagination2(predicate, page, perpage); /// /// Paginate this collection into pages of (x) items each and returns only the first subset. /// /// Type of entity in collection. + /// Type of the property to order by. /// The current collection. /// A filter to apply to the collection before pagination. /// The page to retrieve. @@ -340,10 +354,10 @@ public static PagedResult ToPaginate(this IEnumerable enumerable, Func< /// Ordering type e.g asc or desc /// . [Obsolete("Use .Paginate instead")] - public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate, - int page = _page, int perpage = _perpage, string orderBy = null, string order = "asc") + public static PagedResult ToPaginate(this IEnumerable enumerable, Func predicate, + int page = _page, int perpage = _perpage, Func orderBy = null, string order = "asc") where T : class - => enumerable.ProcessPagination(predicate, page, perpage, orderBy, order); + => enumerable.ProcessPagination2(predicate, page, perpage, orderBy, order); #endregion #region IQueryable Methods @@ -488,22 +502,21 @@ public static PagedResult Paginate(this IQueryable en Items = list }; } - internal static PagedResult ProcessPagination(this IEnumerable enumerable, - Func predicate=null, int page = _page, int perpage = _perpage, string orderBy = null, + internal static PagedResult ProcessPagination2(this IEnumerable enumerable, + Func predicate=null, int page = _page, int perpage = _perpage, Func orderBy = null, string order = "asc") where T : class { int total = 0; IEnumerable query; query = predicate != null ? enumerable.Where(predicate) : enumerable; - if (orderBy.IsValid()) - { - query = order == "asc" ? query.OrderBy(x => x.GetPropertyValue(orderBy)) : - order == "desc" ? query.OrderByDescending(x => x.GetPropertyValue(orderBy)) : query; - } - total = query.CountItems(); + if (orderBy != null) + { + query = order.Equals("asc", StringComparison.OrdinalIgnoreCase) ? query.OrderBy(orderBy) : + order.Equals("desc", StringComparison.OrdinalIgnoreCase) ? query.OrderByDescending(orderBy) : query; + } PagedResult result = new PagedResult() { From 7a59b1e24695d035174194bcfce97f0d58892360 Mon Sep 17 00:00:00 2001 From: tmacharia Date: Tue, 16 Mar 2021 05:24:21 +0300 Subject: [PATCH 5/7] makes the project dependency free --- src/Paginator.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Paginator.csproj b/src/Paginator.csproj index 602fa0c..0e236bf 100644 --- a/src/Paginator.csproj +++ b/src/Paginator.csproj @@ -42,10 +42,6 @@ --> - - - - From 46711e7faf046cf1c3973b33aaf8c94d7f573521 Mon Sep 17 00:00:00 2001 From: tmacharia Date: Tue, 16 Mar 2021 05:25:01 +0300 Subject: [PATCH 6/7] updates nuget packages --- tests/TestCases.csproj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/TestCases.csproj b/tests/TestCases.csproj index bf4d635..1ffc63f 100644 --- a/tests/TestCases.csproj +++ b/tests/TestCases.csproj @@ -5,10 +5,10 @@ - - - - + + + + From 69e5a7e9512b35c8111c72c209f6386581c8f2af Mon Sep 17 00:00:00 2001 From: tmacharia Date: Tue, 16 Mar 2021 05:27:36 +0300 Subject: [PATCH 7/7] version 1.2.7 --- src/Paginator.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Paginator.csproj b/src/Paginator.csproj index 0e236bf..35ab676 100644 --- a/src/Paginator.csproj +++ b/src/Paginator.csproj @@ -27,7 +27,7 @@ - Items (collection of the first 20 records) LinqPaginator - 1.2.6 + 1.2.7 en Fixes bug/issue casting.