Calculating with arrays with NCalc

Topics: General, New Features
Sep 29, 2013 at 3:26 PM
I would like to be able to make that kind of calculation:

Expression e = new Expression("sum(a)");
e.Parameters["a"] = new int[] { 1, 2, 3 };
The answer should be 1+2+3 = 6

I think I need to build a new function, but I don't find how I could handle parameters that are arrays... I tried the following code below, but I did not manage to make it work.

If I find a way to handle arrays, I would like to build a custom function like SUMPRODUCT() in Excel...
Could you tell me if there is a way to do that?

Thanks and best regards.

expression.EvaluateFunction += delegate(string name, FunctionArgs args)
            if (name == "Sum")
                double result = 0;
                foreach (Expression arg in args.Parameters)
                    //if parameter double
                    { result += (double)arg.Evaluate();   }
                    //if parameter is an array
                    else if(arg.GetType()== typeof(Array))
                        foreach(var value in arg)
                        { result += value; }
                args.Result = (double)result;