• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MediatR.CommandQuery / 12567339125

01 Jan 2025 04:52AM UTC coverage: 60.229% (-0.1%) from 60.328%
12567339125

push

github

pwelter34
switch to Testcontainers

402 of 761 branches covered (52.83%)

Branch coverage included in aggregate %.

1282 of 2035 relevant lines covered (63.0%)

19.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.74
/src/MediatR.CommandQuery/Extensions/QueryExtensions.cs
1
using System.Linq.Dynamic.Core;
2
using System.Text;
3

4
using MediatR.CommandQuery.Queries;
5

6
namespace MediatR.CommandQuery.Extensions;
7

8
/// <summary>
9
/// <see cref="IQueryable{T}"/> extensions methods
10
/// </summary>
11
public static class QueryExtensions
12
{
13
    /// <summary>
14
    /// Applies the specified <paramref name="sort"/> to the provided <paramref name="query"/>.
15
    /// </summary>
16
    /// <typeparam name="T">The type of the data in the query</typeparam>
17
    /// <param name="query">The query to apply the sort.</param>
18
    /// <param name="sort">The sort to apply.</param>
19
    /// <returns>An <see cref="IQueryable{T}"/> with the sort applied.</returns>
20
    public static IQueryable<T> Sort<T>(this IQueryable<T> query, EntitySort? sort)
21
    {
22
        if (sort == null)
2!
23
            return query;
2✔
24

25
        return Sort(query, [sort]);
×
26
    }
27

28
    /// <summary>
29
    /// Applies the specified <paramref name="sorts"/> to the provided <paramref name="query"/>.
30
    /// </summary>
31
    /// <typeparam name="T">The type of the data in the query</typeparam>
32
    /// <param name="query">The query to apply the sort.</param>
33
    /// <param name="sorts">The sorts to apply.</param>
34
    /// <returns>An <see cref="IQueryable{T}"/> with the sorts applied.</returns>
35
    public static IQueryable<T> Sort<T>(this IQueryable<T> query, IEnumerable<EntitySort>? sorts)
36
    {
37
        if (sorts?.Any() != true)
44✔
38
            return query;
20✔
39

40
        ArgumentNullException.ThrowIfNull(query);
24✔
41

42
        // Create ordering expression e.g. Field1 asc, Field2 desc
43
        var builder = new StringBuilder();
24✔
44
        foreach (var sort in sorts)
100✔
45
        {
46
            if (builder.Length > 0)
26✔
47
                builder.Append(',');
2✔
48

49
            builder.Append(sort.Name).Append(' ');
26✔
50

51
            var isDescending = !string.IsNullOrWhiteSpace(sort.Direction)
26✔
52
                && sort.Direction.StartsWith(EntitySortDirections.Descending, StringComparison.OrdinalIgnoreCase);
26✔
53

54
            builder.Append(isDescending ? EntitySortDirections.Descending : EntitySortDirections.Ascending);
26✔
55
        }
56

57
        return query.OrderBy(builder.ToString());
24✔
58
    }
59

60
    /// <summary>
61
    /// Applies the specified <paramref name="filter"/> to the provided <paramref name="query"/>.
62
    /// </summary>
63
    /// <typeparam name="T">The type of the data in the query</typeparam>
64
    /// <param name="query">The query to apply the sort.</param>
65
    /// <param name="filter">The filter to apply.</param>
66
    /// <returns>An <see cref="IQueryable{T}"/> with the filter applied.</returns>
67
    public static IQueryable<T> Filter<T>(this IQueryable<T> query, EntityFilter filter)
68
    {
69
        if (filter is null)
62✔
70
            return query;
2✔
71

72
        ArgumentNullException.ThrowIfNull(query);
60✔
73

74
        var builder = new LinqExpressionBuilder();
60✔
75
        builder.Build(filter);
60✔
76

77
        var predicate = builder.Expression;
60✔
78
        var parameters = builder.Parameters.ToArray();
60✔
79

80
        // nothing to filter
81
        if (string.IsNullOrWhiteSpace(predicate))
60✔
82
            return query;
2✔
83

84
        var config = new ParsingConfig
58✔
85
        {
58✔
86
            UseParameterizedNamesInDynamicQuery = true,
58✔
87
        };
58✔
88

89
        return query.Where(config, predicate, parameters);
58✔
90
    }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc