How to use the NCalc switch() extension function

Once you’ve added the PanoramicData.NCalcExtensions nuget to your project, you have access to all kinds of new functions.

One that can make you much more efficient is the switch() function.

For example:

switch(alertLevel, 'critical', 'P1', 'error', 'P2', 'P3')

In this example, we return the string ‘P1’ if the alertLevel is the string ‘critical’, and ‘P2’ if it’s ‘error’. If it is any other string, or null, or not a string, then we return ‘P3’.

Because every NCalc parameter can be another expression, and the switch extension function only evaluates (left to right) if it needs to, we call functions only when needed.

For example:

switch(taskToRun, 'hop', hop(), 'skip', skip(), 'jump', jump(), standStill())

In this example, if taskToRun is null, the default standStill() function is called, avoiding evaluation of hop(), skip() and jump().

switch() is just one of the many functions available as part of the PanoramicData.NCalcExtensions nuget, which is free for commercial and non-commercial use under the MIT licence.