Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Update - I was trying to evaluate an expresion in C# using the DataTable.Compute() method and found that instead of using ternary operator(?,: ), I should've used the IFF(expression, truepart, falsepart). So I'm trying to convert the whole expression into the IFF format and need a little help to construct the logic.

C#
static string IIFApproach()
{
    DataTable dt = new DataTable();
    string formula = "If(1 > 20) then((1 / 1) > 10 and(1 / 1) <= 3500) else (If(1 equals 0) then TRUE else FALSE )";

    formula = formula
        .Replace("If", "IIf(")
        .Replace("then", ",")
        .Replace("equals", "=")
        .Replace("else", ",");


    formula = dt.Compute(formula, "").ToString();

    return formula;
}

I'm struck at the opening and closing of the IFF parenthesis. I have tried to add the starting parenthesis at the time of replacement [.Replace("If", "IIf(")] but I'm unable to put the closing parenthesis at the end of the expression. I know that it can be done using stacks but as I confess I'm not good with data structures. Please help me to construct a logic.


Old Question - I'm trying to evaluate an expression using the Compute() method of DataTable and it shows an error "Cannot interpret token ?". My expression contains ternary operator and i need to solve the expression manually (without using nCalc or other tools). Is there any inbuilt microsoft functionality or any other approach to solve this expression.

C#
DataTable dt = new DataTable();
var result = dt.Compute(" ( 1 > 20 ) ? (( 1 / 1 ) > 10 && ( 1 / 1 ) <= 3500 ) : (  ( 1 == 0) ? 1 : 0 )", null);
Console.WriteLine("The result is" + result);
Posted
Updated 20-Sep-15 7:15am
v3

Right, well obviously that method can't handle C/C++/C# syntax; it's actually pretty limited in what it can do.

Fortunately, .net allows you to compile code at run-time. There are several articles here on CP, including mine: Compiling Source Code from a String[^]
 
Share this answer
 
Comments
Maciej Los 20-Sep-15 12:39pm    
Good idea, a5!
I have added a bit explanation about computed column.
In addition to soultion 1 by PIEBALDconsult[^]...

Computed column of DataTable object can accept quite difficult expression[^], but it is limited to these operators:
AND
OR
NOT
<
>
<=
>=
<>
=
IN
LIKE


The following arithmetic operators are also supported in expressions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus) 
 
Share this answer
 
v2
Comments
iSahilSharma 20-Sep-15 13:17pm    
You are right about the limit of Compute() method but it also supports IIF(expression, truepart, falsepart) [https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx]. I'm trying to convert my expression equivalent to it and trying to build a logic. I've also updated the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900