-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
GridSort.cs
185 lines (163 loc) · 7.8 KB
/
GridSort.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Linq;
using System.Linq.Expressions;
namespace Microsoft.AspNetCore.Components.QuickGrid;
/// <summary>
/// Represents a sort order specification used within <see cref="QuickGrid{TGridItem}"/>.
/// </summary>
/// <typeparam name="TGridItem">The type of data represented by each row in the grid.</typeparam>
public sealed class GridSort<TGridItem>
{
private const string ExpressionNotRepresentableMessage = "The supplied expression can't be represented as a property name for sorting. Only simple member expressions, such as @(x => x.SomeProperty), can be converted to property names.";
private readonly Func<IQueryable<TGridItem>, bool, IOrderedQueryable<TGridItem>> _first;
private List<Func<IOrderedQueryable<TGridItem>, bool, IOrderedQueryable<TGridItem>>>? _then;
private (LambdaExpression, bool) _firstExpression;
private List<(LambdaExpression, bool)>? _thenExpressions;
private IReadOnlyCollection<SortedProperty>? _cachedPropertyListAscending;
private IReadOnlyCollection<SortedProperty>? _cachedPropertyListDescending;
internal GridSort(Func<IQueryable<TGridItem>, bool, IOrderedQueryable<TGridItem>> first, (LambdaExpression, bool) firstExpression)
{
_first = first;
_firstExpression = firstExpression;
_then = default;
_thenExpressions = default;
}
/// <summary>
/// Produces a <see cref="GridSort{T}"/> instance that sorts according to the specified <paramref name="expression"/>, ascending.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public static GridSort<TGridItem> ByAscending<U>(Expression<Func<TGridItem, U>> expression)
=> new((queryable, asc) => asc ? queryable.OrderBy(expression) : queryable.OrderByDescending(expression),
(expression, true));
/// <summary>
/// Produces a <see cref="GridSort{T}"/> instance that sorts according to the specified <paramref name="expression"/>, descending.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public static GridSort<TGridItem> ByDescending<U>(Expression<Func<TGridItem, U>> expression)
=> new((queryable, asc) => asc ? queryable.OrderByDescending(expression) : queryable.OrderBy(expression),
(expression, false));
/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenAscending<U>(Expression<Func<TGridItem, U>> expression)
{
_then ??= new();
_thenExpressions ??= new();
_then.Add((queryable, asc) => asc ? queryable.ThenBy(expression) : queryable.ThenByDescending(expression));
_thenExpressions.Add((expression, true));
_cachedPropertyListAscending = null;
_cachedPropertyListDescending = null;
return this;
}
/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenDescending<U>(Expression<Func<TGridItem, U>> expression)
{
_then ??= new();
_thenExpressions ??= new();
_then.Add((queryable, asc) => asc ? queryable.ThenByDescending(expression) : queryable.ThenBy(expression));
_thenExpressions.Add((expression, false));
_cachedPropertyListAscending = null;
_cachedPropertyListDescending = null;
return this;
}
internal IOrderedQueryable<TGridItem> Apply(IQueryable<TGridItem> queryable, bool ascending)
{
var orderedQueryable = _first(queryable, ascending);
if (_then is not null)
{
foreach (var clause in _then)
{
orderedQueryable = clause(orderedQueryable, ascending);
}
}
return orderedQueryable;
}
internal IReadOnlyCollection<SortedProperty> ToPropertyList(bool ascending)
{
if (ascending)
{
_cachedPropertyListAscending ??= BuildPropertyList(ascending: true);
return _cachedPropertyListAscending;
}
else
{
_cachedPropertyListDescending ??= BuildPropertyList(ascending: false);
return _cachedPropertyListDescending;
}
}
private List<SortedProperty> BuildPropertyList(bool ascending)
{
var result = new List<SortedProperty>
{
new SortedProperty { PropertyName = ToPropertyName(_firstExpression.Item1), Direction = (_firstExpression.Item2 ^ ascending) ? SortDirection.Descending : SortDirection.Ascending }
};
if (_thenExpressions is not null)
{
foreach (var (thenLambda, thenAscending) in _thenExpressions)
{
result.Add(new SortedProperty { PropertyName = ToPropertyName(thenLambda), Direction = (thenAscending ^ ascending) ? SortDirection.Descending : SortDirection.Ascending });
}
}
return result;
}
// Not sure we really want this level of complexity, but it converts expressions like @(c => c.Medals.Gold) to "Medals.Gold"
private static string ToPropertyName(LambdaExpression expression)
{
if (expression.Body is not MemberExpression body)
{
throw new ArgumentException(ExpressionNotRepresentableMessage);
}
// Handles cases like @(x => x.Name)
if (body.Expression is ParameterExpression)
{
return body.Member.Name;
}
// First work out the length of the string we'll need, so that we can use string.Create
var length = body.Member.Name.Length;
var node = body;
while (node.Expression is not null)
{
if (node.Expression is MemberExpression parentMember)
{
length += parentMember.Member.Name.Length + 1;
node = parentMember;
}
else if (node.Expression is ParameterExpression)
{
break;
}
else
{
throw new ArgumentException(ExpressionNotRepresentableMessage);
}
}
// Now construct the string
return string.Create(length, body, (chars, body) =>
{
var nextPos = chars.Length;
while (body is not null)
{
nextPos -= body.Member.Name.Length;
body.Member.Name.CopyTo(chars[nextPos..]);
if (nextPos > 0)
{
chars[--nextPos] = '.';
}
body = (body.Expression as MemberExpression)!;
}
});
}
}