NCALC : Array compare

741 Views Asked by At

I'm trying to compare a array of elements to one value, is there any way to do that using ncalc?

Example:

new Expression(ruleExpression.Replace(" [1,2,3] > 1 and 2 < 3 and 2 == 2").Evaluate(); 

The result that I want is FALSE to ([1,2,3] > 1 ), because not all elements respect the rule.

1

There are 1 best solutions below

0
On

This is a slightly later answer but I thought it still worth answering in case others stumbled across the same issue.

If you are not against migrating away from NCalc then I have created an alternative expression evaluation framework that was originally built to match NCalcs behaviour. It is called Expressive and is available on GitHub or NuGet.

While you can't yet write arrays directly in expressions you can supply them as variables. So to adapt your expression slightly you can supply the following and get the result you are expecting:

new Expression("[array] > 1 and 2 < 3 and 2 == 2").Evaluate(new Dictionary<string, object>()
{
    ["array"] = new int[] {1,2,3}
});