AVG不能正常顯示平均數,怎麼辦
DataTable dt = new DataTable("rawData");
//新建行和列
dt.Columns.Add("Time");
dt.Columns.Add("FeederErr");
dt.Columns.Add("NozzleErr");
dt.Columns.Add("HeadErr");
dt.Columns.Add("AVG");
//獲取數據
DataTable a = BLL.NXTChartMgr.GetNozzleData();
DataTable b = BLL.NXTChartMgr.GetFeederData();
DataTable c = BLL.NXTChartMgr.GetHeadData();
//先循環A,循環一條A,再找B和C,再插入到結果表裡
foreach (DataRow drA in a.Rows)
{
//得到B和C的Err
DataRow drB = b.Select("MACHINE='" + drA["MACHINE"] + "' and uplddte= '" + drA["uplddte"] +"'")[0];
DataRow drC = c.Select("MACHINE='" + drA["MACHINE"] + "' and uplddte= '" + drA["uplddte"] +"'")[0];
//計算avg值
List<double> list = new List<double>();
for (int i = 0; i < a.Rows.Count; i++)
{
double d = (Convert.ToDouble(a.Rows[i]["NozzleErr"]) + Convert.ToDouble(b.Rows[i]["FeederErr"]) + Convert.ToDouble(c.Rows[i]["HeadErr"])) / 3;
list.Add(d);
}
//新建一行
DataRow dr = dt.NewRow();
//賦值
dr["Time"] = drA["uplddte"];
dr["NozzleErr"] = drA["NozzleErr"];
dr["FeederErr"] = drB["FeederErr"];
dr["HeadErr"] = drC["HeadErr"];
dr["AVG"] = list;
//構造dr後,插入dt
dt.Rows.Add(dr);
}
return dt;
datatable不能顯示list泛型或者列表的,你可以把list轉換成string的形式。
如:
string strAvg ="";
foreach(double avg in list)
{
strAvg += avg + ",";
}
strAvg = strAvg.substring(0, strAvg.length-1);
dr["AVG"] = strAvg;