使用DataTable进行统计与分析
1. Expression属性
DataColumn有一个Expression属性,可以接收一个以列名为基础的算术表达式。定义了Expression属性的列称为计算列。
以下代码创建表结构:
如果通过DataRelation定义了数据间的关系,则可以通过计算列对子行进行计算,只需在Expression属性中使用Child关键字,比如假设上面的例子中出现的是另一个DataTable的子表,则在父表中可以建立一个计算列,设置其Expression属性为:
SUM(Child.ItemTotal)
每当子表中ItemTotal列被更新时,父表中对应列自动更新。
2. Compute方法
Datatable的Compute方法可以针对一批数据求和、平均值和方差等。
格式如下:
代码示例如下:
DataColumn有一个Expression属性,可以接收一个以列名为基础的算术表达式。定义了Expression属性的列称为计算列。
以下代码创建表结构:
private void CreateDBStru()
{
DataColumn dc = new DataColumn("ItemName",typeof(string));
dc.ReadOnly = true;
dt.Columns.Add(dc);
dc = new DataColumn("UnitPrice",typeof(float));
dt.Columns.Add(dc);
dc = new DataColumn("Amount",typeof(int));
dt.Columns.Add(dc);
dc = new DataColumn("ItemTotal",typeof(float));
dc.Expression = "UnitPrice*Amount";
dt.Columns.Add(dc);
dt.DefaultView.AllowNew = false;
this.dataGrid1.DataSource = dt;
}
随机添加10条记录{
DataColumn dc = new DataColumn("ItemName",typeof(string));
dc.ReadOnly = true;
dt.Columns.Add(dc);
dc = new DataColumn("UnitPrice",typeof(float));
dt.Columns.Add(dc);
dc = new DataColumn("Amount",typeof(int));
dt.Columns.Add(dc);
dc = new DataColumn("ItemTotal",typeof(float));
dc.Expression = "UnitPrice*Amount";
dt.Columns.Add(dc);
dt.DefaultView.AllowNew = false;
this.dataGrid1.DataSource = dt;
}
private void AddRecord()
{
int count=0;
DataRow dr;
Random ran = new Random();
for(int i=0; i<10; i++)
{
count++;
dr = dt.NewRow();
dr["ItemName"] = "商品" + count.ToString();
dr["UnitPrice"] = ran.NextDouble() * 10;
dr["Amount"] = ran.Next(1,1000);
dt.Rows.Add(dr);
}
}
代码中没有一句是直接给ItemTotal赋值,但其值可由计算机自动生成。{
int count=0;
DataRow dr;
Random ran = new Random();
for(int i=0; i<10; i++)
{
count++;
dr = dt.NewRow();
dr["ItemName"] = "商品" + count.ToString();
dr["UnitPrice"] = ran.NextDouble() * 10;
dr["Amount"] = ran.Next(1,1000);
dt.Rows.Add(dr);
}
}
如果通过DataRelation定义了数据间的关系,则可以通过计算列对子行进行计算,只需在Expression属性中使用Child关键字,比如假设上面的例子中出现的是另一个DataTable的子表,则在父表中可以建立一个计算列,设置其Expression属性为:
SUM(Child.ItemTotal)
每当子表中ItemTotal列被更新时,父表中对应列自动更新。
2. Compute方法
Datatable的Compute方法可以针对一批数据求和、平均值和方差等。
格式如下:
Object sumObj = dt.Compute("SUM(列名)","筛选条件");//筛选条件为空表示全体
代码示例如下:
private void Analyze()
{
DataTable dtAnalyze = dt.Clone();
DataRow dr = dtAnalyze.NewRow();
dr["ItemName"] = "平均:";
dr["UnitPrice"] = dt.Compute("AVG(UnitPrice)","");
dr["Amount"] = dt.Compute("AVG(Amount)","");
dr["ItemTotal"] = dt.Compute("AVG(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dr = dtAnalyze.NewRow();
dr["ItemName"] = "最大值:";
dr["UnitPrice"] = dt.Compute("MAX(UnitPrice)","");
dr["Amount"] = dt.Compute("MAX(Amount)","");
dr["ItemTotal"] = dt.Compute("MAX(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dr = dtAnalyze.NewRow();
dr["ItemName"] = "最小值:";
dr["UnitPrice"] = dt.Compute("MIN(UnitPrice)","");
dr["Amount"] = dt.Compute("MIN(Amount)","");
dr["ItemTotal"] = dt.Compute("MIN(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dr = dtAnalyze.NewRow();
dr["ItemName"] = "方差:";
dr["UnitPrice"] = dt.Compute("VAR(UnitPrice)","");
dr["Amount"] = dt.Compute("VAR(Amount)","");
dr["ItemTotal"] = dt.Compute("VAR(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dr = dtAnalyze.NewRow();
dr["ItemName"] = "求和:";
dr["UnitPrice"] = dt.Compute("SUM(UnitPrice)","");
dr["Amount"] = dt.Compute("SUM(Amount)","");
dr["ItemTotal"] = dt.Compute("SUM(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dtAnalyze.DefaultView.AllowNew = false;
this.dataGrid2.DataSource = dtAnalyze;
}
{
DataTable dtAnalyze = dt.Clone();
DataRow dr = dtAnalyze.NewRow();
dr["ItemName"] = "平均:";
dr["UnitPrice"] = dt.Compute("AVG(UnitPrice)","");
dr["Amount"] = dt.Compute("AVG(Amount)","");
dr["ItemTotal"] = dt.Compute("AVG(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dr = dtAnalyze.NewRow();
dr["ItemName"] = "最大值:";
dr["UnitPrice"] = dt.Compute("MAX(UnitPrice)","");
dr["Amount"] = dt.Compute("MAX(Amount)","");
dr["ItemTotal"] = dt.Compute("MAX(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dr = dtAnalyze.NewRow();
dr["ItemName"] = "最小值:";
dr["UnitPrice"] = dt.Compute("MIN(UnitPrice)","");
dr["Amount"] = dt.Compute("MIN(Amount)","");
dr["ItemTotal"] = dt.Compute("MIN(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dr = dtAnalyze.NewRow();
dr["ItemName"] = "方差:";
dr["UnitPrice"] = dt.Compute("VAR(UnitPrice)","");
dr["Amount"] = dt.Compute("VAR(Amount)","");
dr["ItemTotal"] = dt.Compute("VAR(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dr = dtAnalyze.NewRow();
dr["ItemName"] = "求和:";
dr["UnitPrice"] = dt.Compute("SUM(UnitPrice)","");
dr["Amount"] = dt.Compute("SUM(Amount)","");
dr["ItemTotal"] = dt.Compute("SUM(ItemTotal)","");
dtAnalyze.Rows.Add(dr);
dtAnalyze.DefaultView.AllowNew = false;
this.dataGrid2.DataSource = dtAnalyze;
}