Skip to content

Commit 5f3b6d4

Browse files
committed
feat: can apply filtering and sorting in one command
1 parent f5fc02d commit 5f3b6d4

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace QueryKit.Configuration;
2+
3+
public class QueryKitData
4+
{
5+
public string? Filters { get; set; }
6+
public string? SortOrder { get; set; }
7+
public QueryKitConfiguration? Configuration { get; set; }
8+
}

QueryKit/QueryableExtensions.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
namespace QueryKit;
22

3-
using System.Reflection;
43
using Configuration;
5-
using Microsoft.EntityFrameworkCore;
6-
using Microsoft.EntityFrameworkCore.Infrastructure;
7-
using Microsoft.EntityFrameworkCore.Internal;
8-
using Microsoft.EntityFrameworkCore.Query;
9-
using Microsoft.EntityFrameworkCore.Query.Internal;
104

115
public static class QueryableExtensions
126
{
7+
public static IQueryable<TEntity> ApplyQueryKit<TEntity>(this IQueryable<TEntity> source, QueryKitData queryKitData)
8+
where TEntity : class
9+
{
10+
var appliedQueryable = source;
11+
if (!string.IsNullOrWhiteSpace(queryKitData.Filters))
12+
{
13+
appliedQueryable = appliedQueryable.ApplyQueryKitFilter(queryKitData.Filters, queryKitData.Configuration);
14+
}
15+
16+
if (!string.IsNullOrWhiteSpace(queryKitData.SortOrder))
17+
{
18+
appliedQueryable = appliedQueryable.ApplyQueryKitSort(queryKitData.SortOrder, queryKitData.Configuration);
19+
}
20+
21+
return appliedQueryable;
22+
}
23+
1324
public static IQueryable<TEntity> ApplyQueryKitFilter<TEntity>(this IQueryable<TEntity> source, string filter, IQueryKitConfiguration? config = null)
1425
where TEntity : class
1526
{

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,27 @@ var config = new QueryKitConfiguration(config =>
340340
});
341341
```
342342
343+
## Aggregate QueryKit Application
344+
345+
If you want to apply filtering and sorting in one fell swoop, you can do something like this:
346+
347+
```csharp
348+
var config = new QueryKitConfiguration(config =>
349+
{
350+
config.Property<Person>(x => x.FirstName).HasQueryName("first");
351+
});
352+
var people = _dbContext.People
353+
.ApplyQueryKit(new QueryKitData()
354+
{
355+
Filters = """first == "Jane" && Age > 10""",
356+
SortOrder = "first, Age desc",
357+
Configuration = config
358+
})
359+
.ToList();
360+
```
361+
362+
363+
343364
## Error Handling
344365
345366
If you want to capture errors to easily throw a `400`, you can add error handling around these exceptions:

0 commit comments

Comments
 (0)