-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathQueryableExtensions.cs
More file actions
64 lines (52 loc) · 2.2 KB
/
Copy pathQueryableExtensions.cs
File metadata and controls
64 lines (52 loc) · 2.2 KB
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
namespace QueryKit;
using Configuration;
public static class QueryableExtensions
{
public static IQueryable<TEntity> ApplyQueryKit<TEntity>(this IQueryable<TEntity> source, QueryKitData queryKitData)
where TEntity : class
{
var appliedQueryable = source;
if (!string.IsNullOrWhiteSpace(queryKitData.Filters))
{
appliedQueryable = appliedQueryable.ApplyQueryKitFilter(queryKitData.Filters, queryKitData.Configuration);
}
if (!string.IsNullOrWhiteSpace(queryKitData.SortOrder))
{
appliedQueryable = appliedQueryable.ApplyQueryKitSort(queryKitData.SortOrder, queryKitData.Configuration);
}
return appliedQueryable;
}
public static IQueryable<TEntity> ApplyQueryKitFilter<TEntity>(this IQueryable<TEntity> source, string filter, IQueryKitConfiguration? config = null)
where TEntity : class
{
if (string.IsNullOrWhiteSpace(filter))
{
return source;
}
var expression = FilterParser.ParseFilter<TEntity>(filter, config);
return source.Where(expression);
}
public static IOrderedQueryable<T> ApplyQueryKitSort<T>(this IQueryable<T> queryable, string sortExpression, IQueryKitConfiguration? config = null)
{
var sortLambdas = SortParser.ParseSort<T>(sortExpression, config);
if (sortLambdas.Count == 0)
{
return queryable.OrderBy(x => x);
}
var firstSortInfo = sortLambdas[0];
if (firstSortInfo.Expression != null)
{
var orderedQueryable = firstSortInfo.IsAscending ? queryable.OrderBy(firstSortInfo.Expression) : queryable.OrderByDescending(firstSortInfo.Expression);
for (var i = 1; i < sortLambdas.Count; i++)
{
var sortInfo = sortLambdas[i];
if (sortInfo.Expression != null)
orderedQueryable = sortInfo.IsAscending
? orderedQueryable.ThenBy(sortInfo.Expression)
: orderedQueryable.ThenByDescending(sortInfo.Expression);
}
return orderedQueryable;
}
return queryable.OrderBy(x => x);
}
}