使用DataTable.Compute()方法计算表达式会引发错误 [英] Evaluating an expression using DataTable.Compute() method throws error
问题描述
更新 - 我试图使用 DataTable.Compute()方法评估C#中的表达式,发现不是使用三元运算符(?, :),我应该使用 IFF(表达式,truepart,falsepart)。所以我试图将整个表达式转换为IFF格式,需要一些帮助才能构建逻辑。
static string IIFApproach()
{
DataTable dt = new DataTable();
string formula = If(1>然后((1/1)> 10和(1/1)< = 3500)else(If(1等于0)然后为TRUE,否则为FALSE);
公式=公式
.Replace( 如果, IIf()
.Replace( 然后, ,)
.Replace( 等于, =)
.Replace( else, ,);
formula = dt.Compute(formula, )。ToString ();
返回公式;
}
我'我对IFF括号的开启和关闭感到震惊。我有在替换时添加起始括号[.Replace(If,IIf()]但是我无法将右括号放在表达式的末尾。我知道它可以使用堆栈来完成但是我承认我对数据结构并不擅长。请帮我构建一个逻辑。
旧问题 - 我正在尝试评估表达式使用
DataTable
的 Compute()
方法,它显示错误无法解释令牌?
。我的表达式包含三元运算符,我需要手动解决表达式(不使用nCalc或其他工具)。是否有任何内置的微软功能或任何其他方法来解决这个表达。
DataTable dt = new DataTable();
var result = dt.Compute( (1> 20)?((1/1)> 10&&(1/1)< = 3500):((1 == 0)?1:0),< span class =code-keyword> null );
Console.WriteLine( 结果为 + result);
对,显然该方法无法处理C / C ++ / C#语法;实际上它的功能非常有限。
幸运的是,.net允许你在运行时编译代码。 CP上有几篇文章,包括我的文章:从字符串中编译源代码 [ ^ ]
除了灵魂1之外,还有 PIEBALDconsult [ ^ ]。 ..
计算列DataTable
对象可以接受相当困难的表达式 [ ^ ],但仅限于这些运营商:
AND
OR
NOT
<
>
< =
> =
<>
=
IN
LIKE
表达式中还支持以下算术运算符:
+(加法)
- (减法)
*(乘法)
/(除法)
%(模数)
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.
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.
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);
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[^]
In addition to soultion 1 by PIEBALDconsult[^]...
Computed column ofDataTable
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)
这篇关于使用DataTable.Compute()方法计算表达式会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!