Table of Contents

Class EvaluationHelper

Namespace
NCalc.Helpers
Assembly
NCalc.dll

Provides helper methods for evaluating expressions.

public static class EvaluationHelper
Inheritance
EvaluationHelper
Inherited Members

Methods

Compare(object?, object?, ComparisonType, ComparisonOptions)

public static bool Compare(object? a, object? b, ComparisonType comparisonType, ComparisonOptions options)

Parameters

a object
b object
comparisonType ComparisonType
options ComparisonOptions

Returns

bool

Divide(object?, object?, ExpressionContextBase)

Divides the first value by the second one, with support for timespans as left values.

public static object? Divide(object? leftValue, object? rightValue, ExpressionContextBase context)

Parameters

leftValue object

The left operand.

rightValue object

The right operand.

context ExpressionContextBase

The evaluation context.

Returns

object

The result of the division.

In(object?, object?, ExpressionContextBase)

Determines if the left value is contained within the right value, which must be either an enumerable or a string.

public static bool In(object? rightValue, object? leftValue, ExpressionContextBase context)

Parameters

rightValue object

The right operand.

leftValue object

The left operand.

context ExpressionContextBase

The evaluation context.

Returns

bool

True if the left value is contained within the right value, otherwise false.

Exceptions

NCalcEvaluationException

Thrown when the right value is not an enumerable or a string.

Like(object, object, ExpressionContextBase)

Determines whether a specified string matches a pattern using an event or SQL-like wildcards.

public static bool Like(object value, object pattern, ExpressionContextBase context)

Parameters

value object

The string to be compared against the pattern.

pattern object

The pattern to match. If a default Regex-based matcher is used, '%' matches zero or more characters, and '_' matches exactly one character.

context ExpressionContextBase

The context containing options for the comparison.

Returns

bool

true if the value matches the pattern; otherwise, false.

Remarks

The comparison is case-insensitive if the CaseInsensitiveStringComparer flag is set in the context.

Like(string, string, ExpressionContextBase)

Determines whether a specified string matches a pattern using an event or SQL-like wildcards.

public static bool Like(string value, string pattern, ExpressionContextBase context)

Parameters

value string

The string to be compared against the pattern.

pattern string

The pattern to match. If a default Regex-based matcher is used, '%' matches zero or more characters, and '_' matches exactly one character.

context ExpressionContextBase

The context containing options for the comparison.

Returns

bool

true if the value matches the pattern; otherwise, false.

Remarks

The comparison is case-insensitive if the CaseInsensitiveStringComparer flag is set in the context.

LikeAsync(object, object, ExpressionContextBase, CancellationToken)

Determines whether a specified string matches a pattern using an event or SQL-like wildcards.

public static Task<bool> LikeAsync(object value, object pattern, ExpressionContextBase context, CancellationToken cancellationToken = default)

Parameters

value object

The string to be compared against the pattern.

pattern object

The pattern to match. If a default Regex-based matcher is used, '%' matches zero or more characters, and '_' matches exactly one character.

context ExpressionContextBase

The context containing options for the comparison.

cancellationToken CancellationToken

Returns

Task<bool>

true if the value matches the pattern; otherwise, false.

Remarks

The comparison is case-insensitive if the CaseInsensitiveStringComparer flag is set in the context.

LikeAsync(string, string, ExpressionContextBase, CancellationToken)

Determines whether a specified string matches a pattern using an event or SQL-like wildcards.

public static Task<bool> LikeAsync(string value, string pattern, ExpressionContextBase context, CancellationToken cancellationToken = default)

Parameters

value string

The string to be compared against the pattern.

pattern string

The pattern to match. If a default Regex-based matcher is used, '%' matches zero or more characters, and '_' matches exactly one character.

context ExpressionContextBase

The context containing options for the comparison.

cancellationToken CancellationToken

Returns

Task<bool>

true if the value matches the pattern; otherwise, false.

Remarks

The comparison is case-insensitive if the CaseInsensitiveStringComparer flag is set in the context.

Minus(object?, object?, ExpressionContextBase)

Subtracts the second value from the first one, with support for datetime and timespans

public static object? Minus(object? leftValue, object? rightValue, ExpressionContextBase context)

Parameters

leftValue object

The left operand.

rightValue object

The right operand.

context ExpressionContextBase

The evaluation context.

Returns

object

The result of the substraction.

Multiply(object?, object?, ExpressionContextBase)

Multiplies the first value and the second one, with support for timespans

public static object? Multiply(object? leftValue, object? rightValue, ExpressionContextBase context)

Parameters

leftValue object

The left operand.

rightValue object

The right operand.

context ExpressionContextBase

The evaluation context.

Returns

object

The result of the multiplication.

Plus(object?, object?, ExpressionContextBase)

Adds two values, with special handling for string concatenation based on the context options.

public static object? Plus(object? leftValue, object? rightValue, ExpressionContextBase context)

Parameters

leftValue object

The left operand.

rightValue object

The right operand.

context ExpressionContextBase

The evaluation context.

Returns

object

The result of the addition or string concatenation.

Unary(UnaryExpression, object?, ExpressionContextBase)

Evaluates a unary expression.

public static object? Unary(UnaryExpression expression, object? result, ExpressionContextBase context)

Parameters

expression UnaryExpression

The unary expression to evaluate.

result object

The result of evaluating the operand of the unary expression.

context ExpressionContextBase

The evaluation context.

Returns

object

The result of the unary operation.

Exceptions

InvalidOperationException

Thrown when the unary expression type is unknown.