Evaluate Conditional Expression

I have a scenario in which I am saving my "if" conditions in database as a string. For example:

String condition = "(([age] >= 28) && ([nationality] == 'US'))";

OR

String condition = "([age] >= 28)";

Now, I want to evaluate that the user has input the condition syntactically correct. These are example of incorrect syntax:

String condition = "(([age] >= 28) && ([nationality] == 'US')"; //Missed ')' bracket

String condition = "[age] >= 28)"; //Missed Opening bracket '('

Like we have in Evaluate Query Expression. Might be Expression tress can be helpful. But how? Need help in this regard.

Answers


Take a look at NCalc. It's a framework for evaluating mathematical expressions.

When the expression has a syntax error, the evaluation will throw an EvaluationException.

try
{
    new Expression("(3 + 2").Evaluate();
}
catch(EvaluationException e)
{
    Console.WriteLine("Error catched: " + e.Message);
}

Though, you can also detect syntax errors before the evaluation by using the HasErrors() method.

Expression e = new Expression("a + b * (");
if(e.HasErrors())
{
    Console.WriteLine(e.Error);
}

Visual studio doesn't really know what the strings represent so to my knowledge there is no parsing done within the strings themselves.

Typically when programming with C# and using sql, you'd try to do as much of the calculations as possible in C# itself (if it's feasible select the whole table then deal with the result using C#).

If the database is really slow which is quite often the case, it may be useful writing a SQL Builder class to deal with the hardcoded strings.

If you use neither of these methods, unfortunately the best you can really hope for is runtime exceptions (which isn't optimal for obvious reasons).

EDIT: It seems a SelectQueryBuilder library already exists for the second scenario I suggested.


I found this solution

evaluate an arithmetic expression stored in a string (C#)

SOLUTION:

string conditiontext = "(([age] >= 28) && ([nationality] == \"US\"))";
conditiontext = conditiontext.Replace("[age]", 32)
                             .Replace("[nationality]","US");

/*VsaEngine*/
var engine = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();

/** Result will be either true or false based on evaluation string*/
var result = Microsoft.JScript.Eval.JScriptEvaluate(conditiontext, engine);

[Note: This interface is deprecated. But it evaluates any arithmetic expressions and c# expressions]


Need Your Help

Castle Windsor: Component for which consumer?

c# .net asp.net-mvc inversion-of-control castle-windsor

I'm using ASP.NET MVC, Castle Windsor and Log4Net in a project.

How to rewrite this block of code using a StringBuilder in Java?

java string stringbuilder

Given a word, I've to replace some specific alphabets with some specific letters such as 1 for a, 5 for b etc. I'm using regex for this. I understand that StringBuilder is the best way to deal with...