【问题标题】:Datatable.Compute Runtime ErrorDatatable.Compute 运行时错误
【发布时间】:2013-06-21 10:59:01
【问题描述】:

我想从一列数据表中获取最大值!!

我的数据表有值

Column1                 Date                    Column2  Column3  Column4

Electricity(KWH)    06/Jun/2013 00:00:00    99.00    43.00    56.00
Electricity(KWH)    14/Jun/2013 00:00:00    260.00   48.00    212.00

当我给予时

enter code here

   double a = Convert.ToDouble( dtChart.Compute("MAX(Column2)", "")); 
   variable a has value 260

但是在使用的时候

         for (int i = 3; i < dtChart.Columns.Count; i++)
         {
             string a = dtChart.Columns[i].Caption; //a has value 'Column2';
             double maxYval = Convert.ToDouble(dtChart.Compute("MAX('"+a+"')",""));
         }

它将异常显示为 //将错误显示为“聚合参数中的语法错误:需要一个带有可能的 'Child' 限定符的单列参数。” 怎么办。请帮忙

【问题讨论】:

    标签: c#


    【解决方案1】:

    我怀疑这是因为你在这里引用它:

    "MAX('"+a+"')"
    

    如果你只想得到:

    "MAX(Column2)"
    

    那么你需要:

    "MAX(" + a + ")"
    

    或者:

    string.Format("MAX({0})", a)
    

    【讨论】:

      【解决方案2】:

      如果您想要更易读且不易出错的方法,请使用Linq-To-DataSet

      IEnumerable<DataRow> rows = dtChart.AsEnumerable();
      double col2Max = rows.Max(r => r.Field<double>("Column2"));
      double col3Max = rows.Max(r => r.Field<double>("Column3"));
      double col4Max = rows.Max(r => r.Field<double>("Column4"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多