| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides extension methods for <see cref="IQueryable{T}"/> objects. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class QueryableExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Paginates the queryable. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="queryable">The queryable to paginate.</param> |
| | | 17 | | /// <param name="projection">The projection to apply to the queryable.</param> |
| | | 18 | | /// <param name="pageArgs">The pagination arguments.</param> |
| | | 19 | | /// <typeparam name="T">The type of the queryable.</typeparam> |
| | | 20 | | /// <typeparam name="TTarget">The type of the projection.</typeparam> |
| | | 21 | | /// <returns>A page of the projected queryable.</returns> |
| | | 22 | | public static Page<TTarget> Paginate<T, TTarget>(this IQueryable<T> queryable, Expression<Func<T, TTarget>> projecti |
| | | 23 | | { |
| | 0 | 24 | | if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value); |
| | 0 | 25 | | if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value); |
| | | 26 | | |
| | 0 | 27 | | var count = queryable.Count(); |
| | 0 | 28 | | var results = queryable.Select(projection).ToList(); |
| | 0 | 29 | | return Page.Of(results, count); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Paginates the queryable. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="queryable">The queryable to paginate.</param> |
| | | 36 | | /// <param name="pageArgs">The pagination arguments.</param> |
| | | 37 | | /// <typeparam name="T">The type of the queryable.</typeparam> |
| | | 38 | | /// <returns>A page of the queryable.</returns> |
| | | 39 | | public static Page<T> ToPage<T>(this IQueryable<T> queryable, PageArgs? pageArgs = default) |
| | | 40 | | { |
| | 0 | 41 | | if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value); |
| | 0 | 42 | | if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value); |
| | | 43 | | |
| | 0 | 44 | | var count = queryable.Count(); |
| | 0 | 45 | | var results = queryable.ToList(); |
| | 0 | 46 | | return Page.Of(results, count); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Paginates the queryable. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="queryable">The queryable to paginate.</param> |
| | | 53 | | /// <param name="pageArgs">The pagination arguments.</param> |
| | | 54 | | /// <typeparam name="T">The type of the queryable.</typeparam> |
| | | 55 | | /// <returns>The paginated queryable.</returns> |
| | | 56 | | public static IQueryable<T> Paginate<T>(this IQueryable<T> queryable, PageArgs? pageArgs) |
| | | 57 | | { |
| | 90 | 58 | | if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value); |
| | 90 | 59 | | if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value); |
| | | 60 | | |
| | 49 | 61 | | return queryable; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Orders the queryable by the specified order. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="queryable">The queryable to order.</param> |
| | | 68 | | /// <param name="order">The order to apply to the queryable.</param> |
| | | 69 | | /// <typeparam name="T">The type of the queryable.</typeparam> |
| | | 70 | | /// <typeparam name="TOrderBy">The type of the property to order by.</typeparam> |
| | | 71 | | /// <returns>The ordered queryable.</returns> |
| | | 72 | | public static IQueryable<T> OrderBy<T, TOrderBy>(this IQueryable<T> queryable, OrderDefinition<T, TOrderBy> order) = |
| | 2304 | 73 | | order.Direction == OrderDirection.Ascending |
| | 2304 | 74 | | ? queryable.OrderBy(order.KeySelector) |
| | 2304 | 75 | | : queryable.OrderByDescending(order.KeySelector); |
| | | 76 | | } |