QueryBuilder<T> Class

Builds query objects from query strings with custom property handlers.

public sealed class QueryBuilder<T>

Type parameters

T
The type of object to query against.

Example

var queryBuilder = new QueryBuilder<Person>();
queryBuilder.AddHandler<string>("name", (obj, value) => obj.FullName.Contains(value, StringComparison.OrdinalIgnoreCase));
queryBuilder.AddRangeHandler<int>("age", (obj, range) => range.IsInRange((int)(DateTime.UtcNow - obj.DateOfBirth).TotalDays / 365));
var query = queryBuilder.Build("name:john AND age>=21");
Assert.True(query.Evaluate(new Person("John Doe", new DateTime(2000, 1, 1))));
Assert.False(query.Evaluate(new Person("Jane Doe", new DateTime(2000, 1, 1))));

Constructors

NameDescription
QueryBuilder()Initializes a new instance of QueryBuilder<T> using the system time provider.
QueryBuilder(TimeProvider?)Initializes a new instance of QueryBuilder<T> with a specific time provider for date-related range keywords.

QueryBuilder overloads

QueryBuilder()

QueryBuilder.cs

Initializes a new instance of QueryBuilder<T> using the system time provider.

public QueryBuilder()

QueryBuilder(TimeProvider?)

QueryBuilder.cs

Initializes a new instance of QueryBuilder<T> with a specific time provider for date-related range keywords.

public QueryBuilder(TimeProvider? timeProvider)
Parameters
timeProvider TimeProvider?
The time provider to use, or null to use TimeProvider.System.

Methods

NameDescription
AddHandler(string, Func<T, KeyValueOperator, string, bool>)Registers a handler for a property key.
AddHandler(string, Func<T, string, bool>)Registers a handler for a property key with string value.
AddHandler(string, string?, Func<T, KeyValueOperator, bool>)Registers a handler for a key-value pair that evaluates based on the operator.
AddHandler(string, string?, Func<T, bool>)Registers a handler for a specific key-value pair.
AddHandler<TValue>(string, Func<T, KeyValueOperator, TValue, bool>)Registers a handler for a property key with automatic value parsing.
AddHandler<TValue>(string, Func<T, KeyValueOperator, TValue, bool>, ScalarParser<TValue>?)Registers a handler for a property key with custom value parsing.
AddHandler<TValue>(string, Func<T, TValue, bool>)Registers a handler for a property key with automatic value parsing.
AddHandler<TValue>(string, Func<T, TValue, bool>, ScalarParser<TValue>?)Registers a handler for a property key with custom value parsing.
AddRangeHandler<TValue>(string, Func<T, RangeSyntax<TValue>, bool>)Registers a range handler for a property key with automatic value parsing.
AddRangeHandler<TValue>(string, Func<T, RangeSyntax<TValue>, bool>, ScalarParser<TValue>?)Registers a range handler for a property key with custom value parsing.
Build(string)Builds a query from a query string.
SetTextFilterHandler(Func<T, string, bool>)Sets the handler for free-text search terms without a property key.
SetUnhandledPropertyHandler(UnhandledPropertyDelegate<T>?)Sets the handler for property keys that don't have a registered handler.

AddHandler overloads

AddHandler(string, Func<T, KeyValueOperator, string, bool>)

QueryBuilder.cs

Registers a handler for a property key.

public void AddHandler(string key, Func<T, KeyValueOperator, string, bool> predicate)
Parameters
key string
The property key to handle.
predicate Func<T, KeyValueOperator, string, bool>
The function to evaluate the query.

AddHandler(string, Func<T, string, bool>)

QueryBuilder.cs

Registers a handler for a property key with string value.

public void AddHandler(string key, Func<T, string, bool> predicate)
Parameters
key string
The property key to handle.
predicate Func<T, string, bool>
The function to evaluate the query.

AddHandler(string, string?, Func<T, KeyValueOperator, bool>)

QueryBuilder.cs

Registers a handler for a key-value pair that evaluates based on the operator.

public void AddHandler(string key, string? value, Func<T, KeyValueOperator, bool> predicate)
Parameters
key string
The property key to handle.
value string?
The specific value to match, or null to match any value.
predicate Func<T, KeyValueOperator, bool>
The function to evaluate the query.

AddHandler(string, string?, Func<T, bool>)

QueryBuilder.cs

Registers a handler for a specific key-value pair.

public void AddHandler(string key, string? value, Func<T, bool> predicate)
Parameters
key string
The property key to handle.
value string?
The specific value to match, or null to match any value.
predicate Func<T, bool>
The function to evaluate the query.

AddHandler<TValue>(string, Func<T, KeyValueOperator, TValue, bool>)

QueryBuilder.cs

Registers a handler for a property key with automatic value parsing.

public void AddHandler<TValue>(string key, Func<T, KeyValueOperator, TValue, bool> predicate)
Type parameters
TValue
The type to parse the value as.
Parameters
key string
The property key to handle.
predicate Func<T, KeyValueOperator, TValue, bool>
The function to evaluate the query.

AddHandler<TValue>(string, Func<T, KeyValueOperator, TValue, bool>, ScalarParser<TValue>?)

QueryBuilder.cs

Registers a handler for a property key with custom value parsing.

public void AddHandler<TValue>(string key, Func<T, KeyValueOperator, TValue, bool> predicate, ScalarParser<TValue>? tryParseValue)
Type parameters
TValue
The type to parse the value as.
Parameters
key string
The property key to handle.
predicate Func<T, KeyValueOperator, TValue, bool>
The function to evaluate the query.
tryParseValue ScalarParser<TValue>?
Custom parser for the value, or null to use the default parser.

AddHandler<TValue>(string, Func<T, TValue, bool>)

QueryBuilder.cs

Registers a handler for a property key with automatic value parsing.

public void AddHandler<TValue>(string key, Func<T, TValue, bool> predicate)
Type parameters
TValue
The type to parse the value as.
Parameters
key string
The property key to handle.
predicate Func<T, TValue, bool>
The function to evaluate the query.

AddHandler<TValue>(string, Func<T, TValue, bool>, ScalarParser<TValue>?)

QueryBuilder.cs

Registers a handler for a property key with custom value parsing.

public void AddHandler<TValue>(string key, Func<T, TValue, bool> predicate, ScalarParser<TValue>? tryParseValue)
Type parameters
TValue
The type to parse the value as.
Parameters
key string
The property key to handle.
predicate Func<T, TValue, bool>
The function to evaluate the query.
tryParseValue ScalarParser<TValue>?
Custom parser for the value, or null to use the default parser.

AddRangeHandler overloads

AddRangeHandler<TValue>(string, Func<T, RangeSyntax<TValue>, bool>)

QueryBuilder.cs

Registers a range handler for a property key with automatic value parsing.

public void AddRangeHandler<TValue>(string key, Func<T, RangeSyntax<TValue>, bool> predicate)
Type parameters
TValue
The type to parse the value as.
Parameters
key string
The property key to handle.
predicate Func<T, RangeSyntax<TValue>, bool>
The function to evaluate the range query.

AddRangeHandler<TValue>(string, Func<T, RangeSyntax<TValue>, bool>, ScalarParser<TValue>?)

QueryBuilder.cs

Registers a range handler for a property key with custom value parsing.

public void AddRangeHandler<TValue>(string key, Func<T, RangeSyntax<TValue>, bool> predicate, ScalarParser<TValue>? tryParseValue)
Type parameters
TValue
The type to parse the value as.
Parameters
key string
The property key to handle.
predicate Func<T, RangeSyntax<TValue>, bool>
The function to evaluate the range query.
tryParseValue ScalarParser<TValue>?
Custom parser for the value, or null to use the default parser.

Build(string)

QueryBuilder.cs

Builds a query from a query string.

public Query<T> Build(string query)

Parameters

query string
The query string to parse.

Returns

Query<T> A compiled query that can be evaluated against objects.

Remarks

Values, including date keywords such as today, are parsed when the query is built. Handlers registered or changed afterward do not affect the returned query.

SetTextFilterHandler(Func<T, string, bool>)

QueryBuilder.cs

Sets the handler for free-text search terms without a property key.

public void SetTextFilterHandler(Func<T, string, bool> predicate)

Parameters

predicate Func<T, string, bool>
The function to evaluate free-text queries.

SetUnhandledPropertyHandler(UnhandledPropertyDelegate<T>?)

QueryBuilder.cs

Sets the handler for property keys that don't have a registered handler.

public void SetUnhandledPropertyHandler(UnhandledPropertyDelegate<T>? predicate)

Parameters

predicate UnhandledPropertyDelegate<T>?
The function to evaluate unhandled properties, or null to use default behavior.