Skip to content
Overview

Overview

A typed query model for data lists in .NET. It describes search, filter, sort, paging and facet requests, gives them a URL form, and runs them over in-memory collections or Entity Framework Core, in Blazor, .NET MAUI Blazor Hybrid or an API.

On this page

You describe a row type once, with a schema built from lambda expressions. A request (filter tree, sort, search, paging, facets) is plain data that serializes to JSON, runs against an in-memory list or an IQueryable with the same operators, and has a URL form that survives a round trip. The engine, Klyroo.Query.Core, has no UI or ASP.NET Core dependency and runs wherever .NET 11 runs; Klyroo.Query adds QueryState, which keeps Blazor components in sync with the query string.

Prerequisites

To use the library in an app:

  • The .NET 11 SDK, and an app that targets net11.0. The packages are prereleases, so every dotnet add package command needs --prerelease.
  • Klyroo.Query.Core has no other prerequisite: it runs in any .NET 11 app (console, worker, API, MAUI view model).
  • Klyroo.Query needs a Blazor app (Blazor Web App or .NET MAUI Blazor Hybrid) and AddKlyrooQuery() in Program.cs. It works in every render mode; static server rendering cannot navigate, so links carry the state there.
  • Klyroo.Query.EntityFrameworkCore needs Entity Framework Core 10.0.15 or later and a provider for your database.

To build this repository you need the .NET 11 SDK (a newer SDK works too, see global.json). See Contributing.

Installation

Shell
dotnet add package Klyroo.Query.Core --prerelease                  # the engine, for any .NET 11 project
dotnet add package Klyroo.Query --prerelease                       # adds QueryState for Blazor (brings Klyroo.Query.Core)
dotnet add package Klyroo.Query.EntityFrameworkCore --prerelease   # optional, for EF Core

In a Blazor app, register the service and import the namespace. There is no stylesheet to add.

C#
using Klyroo.Query;

builder.Services.AddKlyrooQuery();
Razor
@using Klyroo.Query

Quick start

Headless first. This runs in a console app, a worker or an API, with only Klyroo.Query.Core installed.

C#
using Klyroo.Query;

Customer[] customers =
[
    new(1, "Alice Martin", "alice@example.com", CustomerStatus.Active, 34),
    new(2, "Bruno Costa", "bruno@example.com", CustomerStatus.Inactive, 51),
    new(3, "Dana Lee", "dana@example.com", CustomerStatus.Active, 28),
    new(4, "Clara Diaz", "clara@example.com", CustomerStatus.Active, 42),
    new(5, "Erik Holm", "erik@example.com", CustomerStatus.Active, 39),
];

var schema = DataListSchema.For<Customer>(s => s
    .RowKey(c => c.Id)
    .Search(c => c.Name, c => c.Email)
    .Field(c => c.Name, f => f.Sortable())
    .Field(c => c.Age, f => f.Sortable().Filter(DataListFilter.Number(min: 0)))
    .Field(c => c.Status, f => f.Filter(DataListFilter.Option()))
    .DefaultSort(c => c.Name));

// Active customers whose name or email contains "ar", oldest first, 25 per page.
var request = new DataListRequest(
    new DataListPaging.Offset(Skip: 0, Take: 25),
    [new DataListSort("age", Descending: true)],
    DataListFilterGroup.And(new FilterRule("status", FilterOperator.Is, new FilterValue.Text("Active"))),
    new DataListSearch("ar"));

DataListResult<Customer> page = schema.ExecuteInMemory(customers, request);

Console.WriteLine($"{page.TotalCount} customers");
foreach (var customer in page.Items)
    Console.WriteLine($"{customer.Name}, {customer.Age}");

public enum CustomerStatus { Active, Inactive }

public sealed record Customer(int Id, string Name, string Email, CustomerStatus Status, int Age);

It prints:

2 customers
Clara Diaz, 42
Alice Martin, 34

Then Blazor. This component binds the q key of the URL to a search box. A link to /customers?q=dan, the Back button and the address bar all change what it shows, and an empty search leaves no key in the URL. It needs an interactive render mode.

Razor
@namespace Samples
@implements IDisposable
@inject QueryState Query

<input type="search" placeholder="Search customers" value="@_search.Value" @oninput="OnInput" />

<ul>
    @foreach (var customer in Matches())
    {
        <li>@customer.Name</li>
    }
</ul>

@code {
    private QueryValue<string> _search = default!;

    protected override void OnInitialized()
    {
        // The default value ("") is left out of the URL. RendererInfo says whether this render can navigate.
        _search = Query.Bind("q", QueryCodecs.String, "", RendererInfo);
        _search.Changed += OnSearchChanged;
    }

    private void OnInput(ChangeEventArgs e) => _search.Value = e.Value?.ToString() ?? "";

    // Changed is raised on the thread that changed the URL, so go through InvokeAsync before rendering.
    private void OnSearchChanged() => _ = InvokeAsync(StateHasChanged);

    private IEnumerable<Customer> Matches() =>
        SampleData.Customers.Where(c => c.Name.Contains(_search.Value, StringComparison.OrdinalIgnoreCase));

    public void Dispose()
    {
        _search.Changed -= OnSearchChanged;
        _search.Dispose();
    }
}

Documentation

Getting started Step-by-step first integration
Guides Task-oriented recipes
Concepts How the library works
Platforms Render modes, .NET MAUI, NativeAOT
API reference Every public type and member
FAQ Troubleshooting and limitations

Compatibility

  • .NET 11 SDK. All three packages target net11.0.
  • Klyroo.Query.Core has no platform restriction. It is marked IsAotCompatible and IsTrimmable, and a console app using the in-memory executor, the URL codecs and the JSON context was published and run with NativeAOT on linux-x64.
  • Klyroo.Query works in Blazor Static SSR, Interactive Server, WebAssembly and Auto. Static SSR cannot navigate, so writes are ignored there and links (HrefWith) carry the state instead. It declares browser, ios, android, maccatalyst and windows as supported platforms for .NET MAUI Blazor Hybrid; this repository does not build or test a MAUI app.
  • Klyroo.Query.EntityFrameworkCore depends on Microsoft.EntityFrameworkCore 10.0.15 or later. The tests of this repository run the IQueryable applier on SQLite.

Contributing

Contributions are welcome. Read CONTRIBUTING.md before opening a pull request, and follow the code of conduct. Report security issues privately as described in SECURITY.md.

License

MIT