【问题标题】:Compare two variables in C# Visual Studio using DataTable.Compute() method使用 DataTable.Compute() 方法比较 C# Visual Studio 中的两个变量
【发布时间】:2021-03-27 09:01:48
【问题描述】:
DataTable dt = new DataTable();
string output = (dt.Compute("3 > 2", String.Empty)).ToString();
MessageBox.Show("Output is " + output);

如果我比较 3 > 2,它给了我 True 但我想实现类似的功能

使用变量如:

字符串输出 = (dt.Compute("a > b", String.Empty)).ToString();

如何将变量添加到表中以实现此目的?

【问题讨论】:

    标签: c# visual-studio visual-studio-2017 visual-studio-2019


    【解决方案1】:

    计算需要一个字符串表达式才能工作,您可以为表达式提供插值变量的值,如

    int a = 3;
    int b = 2;
    string output = (dt.Compute($"{a} > {b}", String.Empty)).ToString();
    

    但是,正如@JohnG 在下面的评论中指出的那样,这里需要提出一个问题。你为什么需要这个?您可以使用简单的代码行获得相同的结果,但性能可能更好

    string output = "false";
    if(a > b)
        output = "true";
    

    【讨论】:

    • 请原谅我的无知,但我不得不问,为什么引入数据表Compute函数与外部值有什么关系?当var result = a > b ? "true" : "false"; 完成同样的事情时,我似乎没有必要为此介绍数据表计算功能。我在这里错过了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-05
    • 2015-08-22
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多