| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | |
| | | 4 | | // ReSharper disable once CheckNamespace |
| | | 5 | | namespace Elsa.Extensions; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for <see cref="IEnumerable{T}"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | [PublicAPI] |
| | | 11 | | public static class EnumerableExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Paginates the enumerable. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="enumerable">The enumerable to paginate.</param> |
| | | 17 | | /// <param name="projection">The projection to apply to the enumerable.</param> |
| | | 18 | | /// <param name="pageArgs">The pagination arguments.</param> |
| | | 19 | | /// <typeparam name="T">The type of the enumerable.</typeparam> |
| | | 20 | | /// <typeparam name="TTarget">The type of the projection.</typeparam> |
| | | 21 | | /// <returns>A page of the projected enumerable.</returns> |
| | | 22 | | public static Page<TTarget> Paginate<T, TTarget>(this IEnumerable<T> enumerable, Func<T, TTarget> projection, PageAr |
| | | 23 | | { |
| | 0 | 24 | | var items = enumerable.ToList(); |
| | 0 | 25 | | var count = items.Count; |
| | | 26 | | |
| | 0 | 27 | | if (pageArgs?.Offset != null) items = items.Skip(pageArgs.Offset.Value).ToList(); |
| | 0 | 28 | | if (pageArgs?.Limit != null) items = items.Take(pageArgs.Limit.Value).ToList(); |
| | | 29 | | |
| | 0 | 30 | | var results = items.Select(projection).ToList(); |
| | 0 | 31 | | return Page.Of(results, count); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Paginates the enumerable. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="enumerable">The enumerable to paginate.</param> |
| | | 38 | | /// <param name="pageArgs">The pagination arguments.</param> |
| | | 39 | | /// <typeparam name="T">The type of the enumerable.</typeparam> |
| | | 40 | | /// <returns>A page of the enumerable.</returns> |
| | | 41 | | public static Page<T> Paginate<T>(this IEnumerable<T> enumerable, PageArgs? pageArgs = default) |
| | | 42 | | { |
| | 0 | 43 | | var items = enumerable.ToList(); |
| | 0 | 44 | | var count = items.Count; |
| | | 45 | | |
| | 0 | 46 | | if (pageArgs?.Offset != null) items = items.Skip(pageArgs.Offset.Value).ToList(); |
| | 0 | 47 | | if (pageArgs?.Limit != null) items = items.Take(pageArgs.Limit.Value).ToList(); |
| | | 48 | | |
| | 0 | 49 | | var results = items.ToList(); |
| | 0 | 50 | | return Page.Of(results, count); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Returns a value indicating whether two lists are equal. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="list1">The first list.</param> |
| | | 57 | | /// <param name="list2">The second list.</param> |
| | | 58 | | /// <typeparam name="T">The type of the items in the lists.</typeparam> |
| | | 59 | | /// <returns>True if the lists are equal, otherwise false.</returns> |
| | 0 | 60 | | public static bool IsEqualTo<T>(this IEnumerable<T> list1, IEnumerable<T> list2) => list1.OrderBy(g => g).SequenceEq |
| | | 61 | | |
| | | 62 | | } |