RunicQueryFields<TRow> class

Namespace Runic.Application.Views in Runic.Application.Views 0.23.0

public sealed class RunicQueryFields<TRow>

The fields a page may sort and filter a RunicQuery by, and how each maps to a LINQ expression of the row. RunicQueryFields.Apply turns a query into Where and OrderBy calls, so an Entity Framework Core or other LINQ provider runs it in the database.

Type parameters

TRow

The row type of the IQueryable.

Remarks

Only registered fields can be used: a query that names another field, or uses an operator a field does not support, fails RunicQueryFields.Validate with ArgumentException, which the page receives as invalid-input. Register fields under the names the page uses, normally the rows' wire names.

A field accepts RunicFilterOperator.Equal for any supported value type, RunicFilterOperator.Range for types with comparison operators (numbers, dates and times, TimeSpan), and RunicFilterOperator.Contains when it is registered with RunicQueryFields.AddText. Values are read from JSON as the field's type: strings, Booleans, integers, floating-point and decimal numbers, Guid, DateTime, DateTimeOffset, DateOnly, TimeOnly, TimeSpan (ISO 8601 or c format) and enums (by name or number).

Constructors

RunicQueryFields

public RunicQueryFields()

No documentation.

Methods

Add

public RunicQueryFields<TRow> Add<TValue>(string name, Expression<Func<TRow, TValue>> selector)

Registers a field that can be sorted by and filtered with equality and, where the type allows, ranges.

Type parameters

TValue

The field's type.

Parameters

name

The field name the page uses.

selector

The field of a row, such as row => row.Created.

Returns

This instance.

AddCustom

public RunicQueryFields<TRow> AddCustom(string name, Func<IQueryable<TRow>, JsonElement, IQueryable<TRow>> apply)

Registers a RunicFilterOperator.Custom filter, the escape hatch for filters the typed model lacks.

Parameters

name

The filter's name.

apply

Applies the filter with its JSON argument (JsonValueKind.Undefined when the page sent none). The argument is untrusted JSON from the page: read only what the filter expects, and throw ArgumentException for anything else. RunicQueryFields.Validate runs the filter once over no rows, so an argument it rejects is invalid-input when the page sets the query.

Returns

This instance.

AddText

public RunicQueryFields<TRow> AddText(string name, Expression<Func<TRow, string?>> selector, bool ignoreCase = true)

Registers a text field, which also supports RunicFilterOperator.Contains.

Parameters

name

The field name the page uses.

selector

The text of a row.

ignoreCase

Whether contains ignores case, by comparing String.ToLower of both, which database providers translate. Otherwise the provider's own comparison applies.

Returns

This instance.

Apply

public IOrderedQueryable<TRow> Apply<TKey>(IQueryable<TRow> rows, RunicQuery query, Expression<Func<TRow, TKey>> key)

Filters and orders rows: RunicQueryFields.Filter, then RunicQueryFields.Sort.

Type parameters

TKey

The row key.

Parameters

rows

The rows.

query

The query.

key

The row key, the last sort key.

Returns

The filtered, ordered rows.

Filter

public IQueryable<TRow> Filter(IQueryable<TRow> rows, RunicQuery query)

Applies query's filters to rows.

Parameters

rows

The rows.

query

The query.

Returns

The filtered rows.

Exceptions

ArgumentException

The query names an unknown field, or a value does not read as the field's type.

Sort

public IOrderedQueryable<TRow> Sort<TKey>(IQueryable<TRow> rows, RunicQuery query, Expression<Func<TRow, TKey>> key)

Orders rows by query's sort keys, then by key, so that ranges of the result are stable.

Type parameters

TKey

The row key.

Parameters

rows

The rows.

query

The query.

key

The row key, the last sort key.

Returns

The ordered rows.

Validate

public void Validate(RunicQuery query)

Checks that every sort key and filter of query names a field that supports it.

Parameters

query

The query.

Exceptions

ArgumentException

The query names an unknown field, or a value does not read as the field's type.