QueryKit 0.5.0-pre001

This is a prerelease version of QueryKit.
There is a newer version of this package available.
See the version list below for details.
dotnet add package QueryKit --version 0.5.0-pre001                
NuGet\Install-Package QueryKit -Version 0.5.0-pre001                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="QueryKit" Version="0.5.0-pre001" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add QueryKit --version 0.5.0-pre001                
#r "nuget: QueryKit, 0.5.0-pre001"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install QueryKit as a Cake Addin
#addin nuget:?package=QueryKit&version=0.5.0-pre001&prerelease

// Install QueryKit as a Cake Tool
#tool nuget:?package=QueryKit&version=0.5.0-pre001&prerelease                

QueryKit 🎛️

QueryKit is a .NET library that makes it easier to query your data by providing a fluent and intuitive syntax for filtering and sorting. The main use case is a lighter weight subset of OData or GraphQL for parsing external filtering and sorting inputs to provide more granular consumption (e.g. a React UI provides filtering controls to filter a worklist). It's inspired by Sieve, but with some differences.

Getting Started

dotnet add package QueryKit

If we wanted to apply a filter to a DbSet called People, we just have to do something like this:

var filterInput = """FirstName == "Jane" && Age > 10""";
var people = _dbContext.People
  	.ApplyQueryKitFilter(filterInput)
  	.ToList();

QueryKit will automatically translate this into an expression for you. You can even customize your property names:

var filterInput = """first == "Jane" && Age > 10""";
var config = new QueryKitConfiguration(config =>
{
    config.Property<Person>(x => x.FirstName).HasQueryName("first");
});
var people = _dbContext.People
  	.ApplyQueryKitFilter(filterInput, config)
  	.ToList();

Sorting works too:

var filterInput = """first == "Jane" && Age > 10""";
var config = new QueryKitConfiguration(config =>
{
    config.Property<Person>(x => x.FirstName).HasQueryName("first");
});
var people = _dbContext.People
  	.ApplyQueryKitFilter(filterInput, config)
  	.ApplyQueryKitSort("first, Age desc", config)
  	.ToList();

And that's the basics! There's no services to inject or global set up to worry about, just apply what you want and call it a day. For a full list of capables, see below.

Filtering

Usage

To apply filters to your queryable, you just need to pass an input string with your filtering input to ApplyQueryKitFilter off of a queryable:

var people = _dbContext.People.ApplyQueryKitFilter("Age < 10").ToList();

You can also pass a configuration like this. More on configuration options below.

var config = new QueryKitConfiguration(config =>
{
    config.Property<SpecialPerson>(x => x.FirstName)
     	 		.HasQueryName("first")
      		.PreventSort();
});
var people = _dbContext.People
  		.ApplyQueryKitFilter(@$"first == "Jane" && Age < 10", config)
  		.ToList();

Logical Operators

When filtering, you can use logical operators && for and as well as || for or. For example:

var input = """FirstName == "Jane" && Age < 10""";
var input = """FirstName == "Jane" || FirstName == "John" """;

Additionally, you can use ^^ for an in operator. You can add an * and use ^^* for case-insensitivity as well:

var input = """(Age ^^ [20, 30, 40]) && (BirthMonth ^^* ["January", "February", "March"]) || (Id ^^ ["6d623e92-d2cf-4496-a2df-f49fa77328ee"])""";

Order of Operations

You can use order of operation with parentheses like this:

var input = """(FirstName == "Jane" && Age < 10) || FirstName == "John" """;

Comparison Operators

There's a wide variety of comparison operators that use the same base syntax as Sieve's operators. To do a case-insensitive operation, just append a * at the end of the operator.

Name Operator Case Insensitive Operator
Equals == ==*
Not Equals != !=*
Greater Than > N/A
Less Than < N/A
Greater Than Or Equal >= N/A
Less Than Or Equal N/A
Starts With _= _=*
Does Not Start With !_= !_=*
Ends With _-= _-=*
Does Not End With !_-= !_-=*
Contains @= @=*
Does Not Contain !@= !@=*
Sounds Like ~~ N/A
Does Not Sound Like !~ N/A

Sounds Like and Does Not Sound Like requires a soundex configuration on your DbContext. For more info see the docs below

Filtering Notes

  • string and guid properties should be wrapped in double quotes

    • null doesn't need quotes: var input = "Title == null";

    • Double quotes can be escaped by using a similar syntax to raw-string literals introduced in C#11:

      var input = """""Title == """lamb is great on a "gee-ro" not a "gy-ro" sandwich""" """"";
      // OR 
      var input = """""""""Title == """"lamb is great on a "gee-ro" not a "gy-ro" sandwich"""" """"""""";
      
  • Dates and times use ISO 8601 format:

    • DateOnly: var filterInput = "Birthday == 2022-07-01";

    • DateTimeOffset:

      • var filterInput = "Birthday == 2022-07-01T00:00:03Z";
    • DateTime: var filterInput = "Birthday == 2022-07-01";

      • var filterInput = "Birthday == 2022-07-01T00:00:03";
      • var filterInput = "Birthday == 2022-07-01T00:00:03+01:00";
    • TimeOnly:

      • var filterInput = "Time == 12:30:00";
      • var filterInput = "Time == 12:30:00.678722";
  • bool properties need to use == true, == false, or the same using the != operator. they can not be standalone properies:

    • var input = """Title == "chicken & waffles" && Favorite""";
    • var input = """Title == "chicken & waffles" && Favorite == true""";
Complex Example
var input = """(Title == "lamb" && ((Age >= 25 && Rating < 4.5) || (SpecificDate <= 2022-07-01T00:00:03Z && Time == 00:00:03)) && (Favorite == true || Email.Value _= "hello@gmail.com"))""";

Settings

Property Settings

Filtering is set up to create an expression using the property names you have on your entity, but you can pass in a config to customize things a bit when needed.

  • HasQueryName() to create a custom alias for a property. For exmaple, we can make FirstName aliased to first.
  • PreventFilter() to prevent filtering on a given property
var input = $"""first == "Jane" || Age > 10""";
var config = new QueryKitConfiguration(config =>
{
    config.Property<SpecialPerson>(x => x.FirstName)
     	 		.HasQueryName("first");
    config.Property<SpecialPerson>(x => x.Age)
      		.PreventFilter();
});
Custom Operators

You can also add custom comparison operators to your config if you'd like:

var config = new QueryKitConfiguration(config =>
{
    config.EqualsOperator = "@@$";
    config.CaseInsensitiveAppendix = "$";
    config.AndOperator = "and";
});

If you want to use it globally, you can make a base implementation like this:

public class CustomQueryKitConfiguration : QueryKitConfiguration
{
    public CustomQueryKitConfiguration(Action<QueryKitSettings>? configureSettings = null)
        : base(settings => 
        {
            settings.EqualsOperator = "eq";
            settings.NotEqualsOperator = "neq";
            settings.GreaterThanOperator = "gt";
            settings.GreaterThanOrEqualOperator = "gte";
            settings.LessThanOperator = "lt";
            settings.LessThanOrEqualOperator = "lte";
            settings.ContainsOperator = "ct";
            settings.StartsWithOperator = "sw";
            settings.EndsWithOperator = "ew";
            settings.NotContainsOperator = "nct";
            settings.NotStartsWithOperator = "nsw";
            settings.NotEndsWithOperator = "new";
            settings.AndOperator = "and";
            settings.OrOperator = "or";
            settings.CaseInsensitiveAppendix = "i";

            configureSettings?.Invoke(settings);
        })
    {
    }
}

// ---

var input = """Title eq$ "Pancakes" and Rating gt 10""";
var config = new CustomQueryKitConfiguration();
var filterExpression = FilterParser.ParseFilter<Recipe>(input, config);

Note Spaces must be used around the comparison operator when using custom values. Title @@$ "titilating"Title@@$"titilating"

Allow Unknown Properties

By default, QueryKit will throw an error if it doesn't recognize a property name, If you want to loosen the reigns here a bit, you can set AllowUnknownProperties to true in your config. When active, unknown properties will be ignored in the expression resolution.

var config = new QueryKitConfiguration(config =>
{
    config.AllowUnknownProperties = true;
});
var filterExpression = FilterParser.ParseFilter<Recipe>(input, config);

Nested Objects

Say we have a nested object like this:


public class SpecialPerson
{
    public Guid Id { get; set; } = Guid.NewGuid();
    public EmailAddress Email { get; set; }
}

public class EmailAddress : ValueObject
{
    public EmailAddress(string? value)
    {
        Value = value;
    }
    
    public string? Value { get; private set; }
}

To actually use the nested properties, you can do something like this:

var input = $"""Email.Value == "{value}" """;

// or with an alias...
var input = $"""email == "hello@gmail.com" """;
var config = new QueryKitConfiguration(config =>
{
    config.Property<SpecialPerson>(x => x.Email.Value).HasQueryName("email");
});

Note, with EF core, your config might look like this:

public sealed class PersonConfiguration : IEntityTypeConfiguration<SpecialPerson>
{
    public void Configure(EntityTypeBuilder<SpecialPerson> builder)
    {
        builder.HasKey(x => x.Id);
        
      // Option 1
        builder.Property(x => x.Email)
            .HasConversion(x => x.Value, x => new EmailAddress(x))
            .HasColumnName("email");      
      
        // Option 2      
        builder.OwnsOne(x => x.Email, opts =>
        {
            opts.Property(x => x.Value).HasColumnName("email");
        }).Navigation(x => x.Email)
            .IsRequired();
    }
}

Warning EF properties configured with HasConversion are not supported at this time -- if this is a blocker for you, i'd love to hear your use case

Sorting

Sorting is a more simplistic flow. It's just an input with a comma delimited list of properties to sort by.

Rules

  • use asc or desc to designate if you want it to be ascending or descending. If neither is used, QueryKit will assume asc
  • You can use Sieve syntax as well by prefixing a property with - to designate it as desc
  • Spaces after commas are optional

So all of these are valid:

var input = "Title";
var input = "Title, Age desc";
var input = "Title desc, Age desc";
var input = "Title, Age";
var input = "Title asc, -Age";
var input = "Title, -Age";

Property Settings

Sorting is set up to create an expression using the property names you have on your entity, but you can pass in a config to customize things a bit when needed.

  • Just as with filtering, HasQueryName() to create a custom alias for a property. For exmaple, we can make FirstName aliased to first.
  • PreventSort() to prevent filtering on a given property
var input = $"""Age desc, first"";
var config = new QueryKitConfiguration(config =>
{
    config.Property<SpecialPerson>(x => x.FirstName)
          .HasQueryName("first")
          .PreventSort();
});

Aggregate QueryKit Application

If you want to apply filtering and sorting in one fell swoop, you can do something like this:

var config = new QueryKitConfiguration(config =>
{
    config.Property<Person>(x => x.FirstName).HasQueryName("first");
});
var people = _dbContext.People
  	.ApplyQueryKit(new QueryKitData() 
        {
            Filters = """first == "Jane" && Age > 10""",
            SortOrder = "first, Age desc",
            Configuration = config
        })
  	.ToList();

Error Handling

If you want to capture errors to easily throw a 400, you can add error handling around these exceptions:

  • A FilterParsingException will be thrown when there is an invalid operator or bad syntax is used (e.g. not using double quotes around a string or guid).
  • An UnknownFilterPropertyException will be thrown if a property is not recognized during filtering
  • A SortParsingException will be thrown if a property or operation is not recognized during sorting
  • A QueryKitDbContextTypeException will be thrown when trying to use a DbContext specific workflow without passing that context (e.g. SoundEx)
  • A SoundsLikeNotImplementedException will be thrown when trying to use soundex on a DbContext that doesn't have it implemented.

SoundEx

The Sounds Like and Does Not Sound Like operators require a soundex configuration on any DbContext that contain your DbSet being filtered on. Something like the below should work. The SoundsLike method does not need to implement anything and is just used as a pointer to the db method.

public class ExampleDbContext : DbContext
{
    public ExampleDbContext(DbContextOptions<TestingDbContext> options)
        : base(options)
    {
    }
    
    [DbFunction (Name = "SOUNDEX", IsBuiltIn = true)]
    public static string SoundsLike(string query) => throw new NotImplementedException();

    public DbSet<People> MyPeople { get; set; }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.HasPostgresExtension("fuzzystrmatch");
    }
}

⭐️ Note that with Postgres, something like modelBuilder.HasPostgresExtension("fuzzystrmatch"); will need to be added like the example along with a migration for adding the extension.

You can even use this on a normal IQueryable like this:

var waffleRecipes = _dbContext.MyPeople
  .Where(x => ExampleDbContext.SoundsLike(x.LastName) == ExampleDbContext.SoundsLike("devito"))
  .ToList();

Usage

Once your DbContext is configured to allow soundex, you'll need to provide that DbContext type in your QueryKit config. This is because, as of now, there is no reliable way to get the DbContext from an IQueryable.

var input = $"""LastName ~~ "devito" """;

// Act
var queryablePeople = testingServiceScope.DbContext().People;
var appliedQueryable = queryablePeople.ApplyQueryKitFilter(input, new QueryKitConfiguration(o =>
{
    o.DbContextType = typeof(TestingDbContext);
}));
var people = await appliedQueryable.ToListAsync();
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on QueryKit:

Package Downloads
KayordKit

KayordKit to guide the way

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.4 532 11/2/2024
1.4.3 2,025 9/7/2024
1.4.2 1,482 8/14/2024
1.4.1 2,108 7/19/2024
1.4.0 5,525 6/23/2024
1.3.3 3,874 4/16/2024
1.3.2 606 4/12/2024
1.3.1 100 4/12/2024
1.3.0 1,546 3/24/2024
1.2.1 9,874 1/20/2024
1.2.0 321 1/16/2024
1.1.0 6,641 12/22/2023
1.0.0 423 11/15/2023
0.6.1 1,266 9/8/2023
0.6.0 671 7/24/2023
0.5.0-pre002 161 7/19/2023
0.5.0-pre001 98 7/19/2023
0.4.0 191 7/11/2023
0.3.0 144 6/2/2023
0.2.0 151 5/1/2023
0.1.1 142 5/1/2023
0.1.0 152 4/29/2023