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 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.
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.
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.