Datatable 列查询,统计值

时间:2022-07-13 13:00:43

Column 列查询,如下:

            var dt = CommonUtil.ToDataTable(dataJson);
//判断是否有当前日期数据
var systemDateTime = new CommonProperty().SystemDateTime;
DataRow[] drArray = dt.Select(string.Format("COL_ActDate>='{0}' and COL_ActDate<'{1}'",
systemDateTime.Date.ToString("yyyy-MM-dd HH:mm:ss"),
systemDateTime.Date.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss")));
if (!drArray.Any()) throw new Exception("数据中没有当前日期数据无法保存"); var dicUpdate=new Dictionary<decimal, decimal?>();
var addList = new List<CraftParaActEntity>();
foreach (var column in dt.Columns)
{
string strColumn = column.ToString();
if (!strColumn.Contains("COL_Value_")) continue;
string strColumnId = string.Format("COL_CraftParaActId_{0}", strColumn.Replace("COL_Value_", ""));                                    
         DataRow[] drIdArray = dt.Select(string.Format("{0} is not null", strColumnId));               
          decimal columnId = decimal.Parse(drIdArray[0][strColumnId].ToString());                 
          decimal craftParaId = CraftParaActDALService.QuerySingleCraftParaAct(columnId).CraftParaId;
}

计算列中的统计值,使用聚合函数,此列应是int,decimal可计算的数据类型,如下:

                var maxValue = dt.Compute(string.Format("max({0})", strColumn), "true");
var minValue = dt.Compute(string.Format("min({0})", strColumn), "true");
var avgValue = dt.Compute(string.Format("avg({0})", strColumn), "true");