Skip to content

OnixLabs.Core.Linq.ExpressionExtensions

matthew edited this page May 27, 2026 · 1 revision

ExpressionExtensions Class

Provides extension methods for Expression<TDelegate> instances, enabling boolean predicate expressions to be combined with logical AND / OR and negated with logical NOT.

Namespace OnixLabs.Core.Linq
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → ExpressionExtensions

Definition

[EditorBrowsable(EditorBrowsableState.Never)]
public static class ExpressionExtensions

Note ExpressionExtensions is decorated with [EditorBrowsable(EditorBrowsableState.Never)]. Although the class is public, it is hidden from IntelliSense completion lists so that only the extension methods themselves appear on their receiver types. Bring the extensions into scope with using OnixLabs.Core.Linq;.

This class uses a C# 14 extension block (extension<T>(Expression<Func<T, bool>> receiver) { ... }), so the receiver type appears in the block declaration rather than as a this parameter. The signatures shown below are the call-site signatures as consumed by user code.

Remarks

All three methods in this class operate over a receiver of type Expression<Func<T, bool>> — a boolean predicate expression. When combining or negating expressions, each method introduces a fresh, shared parameter (named $param) so that both operands reference the same parameter node, producing a valid combined expression tree. Internal or nested lambda parameters that do not match the replaced parameter remain untouched.

These composable predicates underpin the Specification<T> pattern: a Specification<T> wraps an Expression<Func<T, bool>> as its Criteria property, and specification combinators (And, Or, Not) delegate to the corresponding ExpressionExtensions methods.

Methods

Method Returns Description
And(Expression<Func<T, bool>>) Expression<Func<T, bool>> Combines two predicate expressions with logical AND.
Or(Expression<Func<T, bool>>) Expression<Func<T, bool>> Combines two predicate expressions with logical OR.
Not() Expression<Func<T, bool>> Negates the predicate expression with logical NOT.

Examples

using OnixLabs.Core.Linq;
using System;
using System.Linq.Expressions;

Expression<Func<int, bool>> isPositive = x => x > 0;
Expression<Func<int, bool>> isEven = x => x % 2 == 0;

Expression<Func<int, bool>> isPositiveAndEven = isPositive.And(isEven);
Expression<Func<int, bool>> isPositiveOrEven  = isPositive.Or(isEven);
Expression<Func<int, bool>> isNotPositive     = isPositive.Not();

Func<int, bool> predicate = isPositiveAndEven.Compile();
bool result = predicate(4); // True

And(Expression<Func<T, bool>>)

public Expression<Func<T, bool>> And(Expression<Func<T, bool>> other)

Combines two Expression<Func<T, bool>> instances into a single expression using the logical AND operator (AndAlso).

Parameter Type Description
other Expression<Func<T, bool>> The other (right-hand) expression to combine.
Type Parameter Description
T The underlying type of the (left-hand) expression.

Returns — A new Expression<Func<T, bool>> that combines the two expressions using the logical AND operator.


Or(Expression<Func<T, bool>>)

public Expression<Func<T, bool>> Or(Expression<Func<T, bool>> other)

Combines two Expression<Func<T, bool>> instances into a single expression using the logical OR operator (OrElse).

Parameter Type Description
other Expression<Func<T, bool>> The other (right-hand) expression to combine.
Type Parameter Description
T The underlying type of the (left-hand) expression.

Returns — A new Expression<Func<T, bool>> that combines the two expressions using the logical OR operator.


Not()

public Expression<Func<T, bool>> Not()

Negates the current Expression<Func<T, bool>> instance using the logical NOT operator.

Type Parameter Description
T The underlying type of the expression.

Returns — A new Expression<Func<T, bool>> that negates the current expression using the logical NOT operator.


See Also

ONIXLabs .NET Library


Clone this wiki locally