Search code examples
c#relational-operators

evaluate relational operator from a string


I have relational expressions stored in a database, that i have as strings in an iOS app. I would like to evaluate the conditions within the strings in C#, similar to the logic in the following psudo code:

string str1= "x > 0";
string str2= "y < 1";

int x = 1;
int y=0;

if(str1 && str2)
{
        //do stuff
}

Solution

  • If the expressions are simple like the one in the example, then you can simply parse them. But if you have more complex expressions in mind, then I recommend taking a look at C# Expression Trees. The documentation does a good job of explaining it.

    A much easier method would be to use library like: https://github.com/davideicardi/DynamicExpresso