[英]calculate xor expression using DataTable in C#
I want to calculate a logical expression like (true xor false)
I used DataTable but it dose not support xor
.我想计算一个逻辑表达式,例如
(true xor false)
我使用了 DataTable 但它不支持xor
。 How can I calculate xor
expressions?如何计算
xor
或表达式?
System.Data.DataTable table = new System.Data.DataTable();
var result = table.Compute("( False xor True) ", "");
You are trying to parse a string containing a C# logical expression.您正在尝试解析包含 C# 逻辑表达式的字符串。 There are multiple ways of doing it:
有多种方法可以做到:
Since your logical expression only uses different combinations of true , false , and , or and xor , with parenthesis, you could roll out your own parser.由于您的逻辑表达式仅使用带括号的true 、 false和 and或 or和xor的不同组合,因此您可以推出自己的解析器。 Should be a pretty slim function.
应该是相当纤薄的 function。
If you're using this in the context of a DataTable
as evident from the question tags, there is a built-in function called Compute
that can take simple expressions and evaluate them for you.如果您在
DataTable
的上下文中使用它,从问题标签中可以看出,有一个名为Compute
的内置 function 可以采用简单的表达式并为您评估它们。 XOR is not available by default in the list of supported operators, but you could write an equivalent expression using not equal to operator (<>).默认情况下,XOR 在支持的运算符列表中不可用,但您可以使用不等于运算符 (<>) 编写等效表达式。 Remmber A ^ B is equivalent to (A <> B) in the context of DataTable's boolean expresions.
记住A ^ B在 DataTable 的 boolean 表达式的上下文中等价于(A <> B) 。
You could use Roslyn's built-in capabilities and parse and evaluate literally any C# expression at runtime.您可以使用 Roslyn 的内置功能并在运行时解析和评估任何 C# 表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.