< Summary

Information
Class: Elsa.Extensions.QueryableExtensions
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/QueryableExtensions.cs
Line coverage
37%
Covered lines: 6
Uncovered lines: 10
Coverable lines: 16
Total lines: 76
Line coverage: 37.5%
Branch coverage
38%
Covered branches: 10
Total branches: 26
Branch coverage: 38.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Paginate(...)0%7280%
ToPage(...)0%7280%
Paginate(...)100%88100%
OrderBy(...)100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/QueryableExtensions.cs

#LineLine coverage
 1using System.Linq.Expressions;
 2using Elsa.Common.Entities;
 3using Elsa.Common.Models;
 4
 5// ReSharper disable once CheckNamespace
 6namespace Elsa.Extensions;
 7
 8/// <summary>
 9/// Provides extension methods for <see cref="IQueryable{T}"/> objects.
 10/// </summary>
 11public 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    {
 024        if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value);
 025        if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value);
 26
 027        var count = queryable.Count();
 028        var results = queryable.Select(projection).ToList();
 029        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    {
 041        if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value);
 042        if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value);
 43
 044        var count = queryable.Count();
 045        var results = queryable.ToList();
 046        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    {
 9058        if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value);
 9059        if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value);
 60
 4961        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) =
 230473        order.Direction == OrderDirection.Ascending
 230474            ? queryable.OrderBy(order.KeySelector)
 230475            : queryable.OrderByDescending(order.KeySelector);
 76}