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
| Name | Description |
|---|---|
| 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(TimeProvider?, TimeZoneInfo?) | Initializes a new instance of QueryBuilder<T> with a specific time provider and time zone for date-related range keywords. |
QueryBuilder overloads
QueryBuilder()
QueryBuilder.csInitializes a new instance of QueryBuilder<T> using the system time provider.
public QueryBuilder()QueryBuilder(TimeProvider?)
QueryBuilder.csInitializes a new instance of QueryBuilder<T> with a specific time provider for date-related range keywords.
public QueryBuilder(TimeProvider? timeProvider)Parameters
timeProviderTimeProvider?- The time provider to use, or
nullto useTimeProvider.System.
QueryBuilder(TimeProvider?, TimeZoneInfo?)
QueryBuilder.csInitializes a new instance of QueryBuilder<T> with a specific time provider and time zone for date-related range keywords.
public QueryBuilder(TimeProvider? timeProvider, TimeZoneInfo? timeZone)Parameters
timeProviderTimeProvider?- The time provider to use, or
nullto useTimeProvider.System. timeZoneTimeZoneInfo?- The time zone that gives the day boundaries of date keywords, such as
todayand@today-3d, ornullto useTimeZoneInfo.Utc.
Properties
| Name | Description |
|---|---|
| Strict | Gets or sets whether QueryBuilder<T>.Build throws when the query has diagnostics. |
Strict
QueryBuilder.csGets or sets whether QueryBuilder<T>.Build throws when the query has diagnostics.
public bool Strict { get; set; }Property value
bool true to throw an InvalidQueryException for a query with diagnostics, such as an unbalanced parenthesis or an unknown key; false (the default) to read the query leniently and report the diagnostics in Query<T>.Diagnostics.
Methods
| Name | Description |
|---|---|
| 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.csRegisters a handler for a property key.
Parameters
keystring- The property key to handle.
predicateFunc<T, KeyValueOperator, string, bool>- The function to evaluate the query.
AddHandler(string, Func<T, string, bool>)
QueryBuilder.csAddHandler(string, string?, Func<T, KeyValueOperator, bool>)
QueryBuilder.csRegisters a handler for a key-value pair that evaluates based on the operator.
Parameters
keystring- The property key to handle.
valuestring?- The specific value to match, or null to match any value.
predicateFunc<T, KeyValueOperator, bool>- The function to evaluate the query.
AddHandler(string, string?, Func<T, bool>)
QueryBuilder.csAddHandler<TValue>(string, Func<T, KeyValueOperator, TValue, bool>)
QueryBuilder.csRegisters 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
keystring- The property key to handle.
predicateFunc<T, KeyValueOperator, TValue, bool>- The function to evaluate the query.
AddHandler<TValue>(string, Func<T, KeyValueOperator, TValue, bool>, ScalarParser<TValue>?)
QueryBuilder.csRegisters 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
keystring- The property key to handle.
predicateFunc<T, KeyValueOperator, TValue, bool>- The function to evaluate the query.
tryParseValueScalarParser<TValue>?- Custom parser for the value, or null to use the default parser.
AddHandler<TValue>(string, Func<T, TValue, bool>)
QueryBuilder.csRegisters a handler for a property key with automatic value parsing.
Type parameters
TValue- The type to parse the value as.
Parameters
keystring- The property key to handle.
predicateFunc<T, TValue, bool>- The function to evaluate the query.
AddHandler<TValue>(string, Func<T, TValue, bool>, ScalarParser<TValue>?)
QueryBuilder.csRegisters 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
keystring- The property key to handle.
predicateFunc<T, TValue, bool>- The function to evaluate the query.
tryParseValueScalarParser<TValue>?- Custom parser for the value, or null to use the default parser.
AddRangeHandler overloads
AddRangeHandler<TValue>(string, Func<T, RangeSyntax<TValue>, bool>)
QueryBuilder.csRegisters 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
keystring- The property key to handle.
predicateFunc<T, RangeSyntax<TValue>, bool>- The function to evaluate the range query.
AddRangeHandler<TValue>(string, Func<T, RangeSyntax<TValue>, bool>, ScalarParser<TValue>?)
QueryBuilder.csRegisters 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
keystring- The property key to handle.
predicateFunc<T, RangeSyntax<TValue>, bool>- The function to evaluate the range query.
tryParseValueScalarParser<TValue>?- Custom parser for the value, or null to use the default parser.
Build(string)
QueryBuilder.csBuilds a query from a query string.
Parameters
querystring- The query string to parse.
Returns
Query<T> A compiled query that can be evaluated against objects.
Exceptions
QueryTooComplexException- The query is too complex to be evaluated.
InvalidQueryExceptionQueryBuilder<T>.Strictistrueand the query has diagnostics.
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.csSets the handler for free-text search terms without a property key.
Parameters
predicateFunc<T, string, bool>- The function to evaluate free-text queries.
SetUnhandledPropertyHandler(UnhandledPropertyDelegate<T>?)
QueryBuilder.csSets the handler for property keys that don't have a registered handler.
public void SetUnhandledPropertyHandler(UnhandledPropertyDelegate<T>? predicate)Parameters
predicateUnhandledPropertyDelegate<T>?- The function to evaluate unhandled properties, or null to use default behavior.