ExpressionQueryBuilder<T> Class
Builds expression-based queries from query strings for use with IQueryable and EF Core.
public sealed class ExpressionQueryBuilder<T>Type parameters
T- The type of object to query against.
Remarks
Unlike QueryBuilder<T> which uses Predicate<T> for in-memory evaluation, this builder creates Expression<TDelegate> objects that can be translated to SQL by EF Core.
Example
var queryBuilder = new ExpressionQueryBuilder<Person>();
queryBuilder.AddHandler("name", item => item.FullName);
queryBuilder.AddHandler<int>("age", item => item.Age);
var query = queryBuilder.Build("name:John AND age>=21");
// Apply works on any IQueryable<T>, such as an Entity Framework Core DbSet<T>
var people = new[] { new Person("John Doe", 30), new Person("John Smith", 18), new Person("Jane Doe", 25) }.AsQueryable();
var results = query.Apply(people).ToList();
Assert.Equal("John Doe", Assert.Single(results).FullName);Constructors
| Name | Description |
|---|---|
| ExpressionQueryBuilder() | Initializes a new instance of ExpressionQueryBuilder<T> using the system time provider. |
| ExpressionQueryBuilder(TimeProvider?) | Initializes a new instance of ExpressionQueryBuilder<T> with a specific time provider for date-related range keywords. |
ExpressionQueryBuilder overloads
ExpressionQueryBuilder()
ExpressionQueryBuilder.csInitializes a new instance of ExpressionQueryBuilder<T> using the system time provider.
public ExpressionQueryBuilder()ExpressionQueryBuilder(TimeProvider?)
ExpressionQueryBuilder.csInitializes a new instance of ExpressionQueryBuilder<T> with a specific time provider for date-related range keywords.
public ExpressionQueryBuilder(TimeProvider? timeProvider)Parameters
timeProviderTimeProvider?- The time provider to use, or
nullto useTimeProvider.System.
Methods
| Name | Description |
|---|---|
| AddHandler(string, KeyValueOperator, Func<string, Expression<Func<T, bool>>>) | Registers a custom expression handler for a property key and operator. |
| AddHandler(string, Func<string, Expression<Func<T, bool>>>) | Registers a custom expression handler for a property key with equality operator. |
| AddHandler(string, Expression<Func<T, string?>>, StringComparison?) | Registers a handler for a string property using a "contains" comparison. |
| AddHandler<TValue>(string, Expression<Func<T, TValue>>, ScalarParser<TValue>?) | Registers a handler for a property with automatic comparison operators. |
| Build(string) | Builds a query from a query string. |
| SetFreeTextHandler(FreeTextExpressionHandler<T>?) | Sets the handler for free-text search terms without a property key. |
| SetUnhandledPropertyHandler(UnhandledPropertyExpressionHandler<T>?) | Sets the handler for property keys that don't have a registered handler. |
AddHandler overloads
AddHandler(string, KeyValueOperator, Func<string, Expression<Func<T, bool>>>)
ExpressionQueryBuilder.csRegisters a custom expression handler for a property key and operator.
public void AddHandler(string key, KeyValueOperator op, Func<string, Expression<Func<T, bool>>> handler)Parameters
keystring- The property key to handle.
opKeyValueOperator- The operator to handle.
handlerFunc<string, Expression<Func<T, bool>>>- Function that creates the filter expression from the query value.
AddHandler(string, Func<string, Expression<Func<T, bool>>>)
ExpressionQueryBuilder.csRegisters a custom expression handler for a property key with equality operator.
Parameters
keystring- The property key to handle.
handlerFunc<string, Expression<Func<T, bool>>>- Function that creates the filter expression from the query value.
AddHandler(string, Expression<Func<T, string?>>, StringComparison?)
ExpressionQueryBuilder.csRegisters a handler for a string property using a "contains" comparison.
public void AddHandler(string key, Expression<Func<T, string?>> selector, StringComparison? comparisonType = null)Parameters
keystring- The property key to handle.
selectorExpression<Func<T, string?>>- Expression selecting the string property.
comparisonTypeStringComparison?- The string comparison to use, or
nullto emit a plainString.Containscall. Most LINQ providers, including Entity Framework Core, cannot translate theStringComparisonoverload ofString.Contains, so passing a value here makes the query usable in memory only. When leftnull, case sensitivity is decided by the underlying provider — the database collation for EF Core, and an ordinal comparison for LINQ to Objects.
AddHandler<TValue>(string, Expression<Func<T, TValue>>, ScalarParser<TValue>?)
ExpressionQueryBuilder.csRegisters a handler for a property with automatic comparison operators.
public void AddHandler<TValue>(string key, Expression<Func<T, TValue>> selector, ScalarParser<TValue>? tryParseValue = null)Type parameters
TValue- The type of the property.
Parameters
keystring- The property key to handle.
selectorExpression<Func<T, TValue>>- Expression selecting the property.
tryParseValueScalarParser<TValue>?- Custom parser for the value, or null to use the default parser.
Build(string)
ExpressionQueryBuilder.csBuilds a query from a query string.
public ExpressionQuery<T> Build(string query)Parameters
querystring- The query string to parse.
Returns
ExpressionQuery<T> A compiled expression query that can be applied to an IQueryable<T>.
SetFreeTextHandler(FreeTextExpressionHandler<T>?)
ExpressionQueryBuilder.csSets the handler for free-text search terms without a property key.
public void SetFreeTextHandler(FreeTextExpressionHandler<T>? handler)Parameters
handlerFreeTextExpressionHandler<T>?- The function to create filter expressions for free-text queries.
SetUnhandledPropertyHandler(UnhandledPropertyExpressionHandler<T>?)
ExpressionQueryBuilder.csSets the handler for property keys that don't have a registered handler.
public void SetUnhandledPropertyHandler(UnhandledPropertyExpressionHandler<T>? handler)Parameters
handlerUnhandledPropertyExpressionHandler<T>?- The function to create filter expressions for unhandled properties.