DataTable.Compute方法使用实例

本文介绍使用C#中的DataTable进行数据计算的高级技巧,包括常量计算、聚合函数的应用、计算列的实现方法及父子表间的复杂计算。通过示例代码详细展示了如何利用DataTable减少SQL语句并提高服务器计算效率。
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Console11
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Data.DataTable table = new DataTable();


            //计算常量,可以没有初始化列
            object test = table.Compute("1+1", "");
            Console.WriteLine(test);

            string a = "123";
            System.Double b=123;
            decimal c=123m;
            Console.WriteLine((float)a);
            //test=2;

            test = table.Compute("1+1", "false");
            Console.WriteLine(test);
            //test=2;常数计算和filter无关

            test = table.Compute("abs(1)", "");
            Console.WriteLine(test);
            //test=null,不知道为这个什么没有报错,而且返回null,其他的数学函数都会抱错

            test = table.Compute("2%2", "");
            Console.WriteLine(test);
            //test=0;
            //其他函数参考下面的计算列

 

            //初始化datatale
            table.Columns.Add("id", typeof(string));
            table.Columns.Add("value", typeof(int));
            for (int i = 1; i <= 10; i++)
            {
                System.Data.DataRow dRow = table.NewRow();
                dRow["id"] = "id" + i.ToString();
                dRow["value"] = i;
                table.Rows.Add(dRow);
            }

 

            //test = table.Compute("value+1", "true");
            /**/
            抛出异常,这里必须是聚合函数

 

            //*************************************支持的聚合函数**********************//

            //求数量
            test = table.Compute("count(id)", "false");
            Console.WriteLine(test);
            //test=0;

            test = table.Compute("count(id)", "true");
            Console.WriteLine(test);
            //test=10;

 

            //求和
            test = table.Compute("sum(value)", "");
            Console.WriteLine(test);
            //test=55;

            //test = table.Compute("sum(id)","");
            /**/
            抛出异常,这里不能是string


            //平均
            test = table.Compute("avg(value)", "");
            Console.WriteLine(test);
            //test=5;


            //最小
            test = table.Compute("min(value)", "");
            Console.WriteLine(test);
            //test=1;

            //最大
            test = table.Compute("max(value)", "");
            Console.WriteLine(test);
            //test=10;

            //统计标准偏差
            test = table.Compute("StDev(value)", "");
            Console.WriteLine(test);
            //test=3.02765035409749

            //统计方差
            test = table.Compute("Var(value)", "");
            Console.WriteLine(test);
            //test=9.16666666666667


            //复杂计算
            test = table.Compute("max(value)/sum(value)", "");
            Console.WriteLine(test);
            //test=0.181818181818182

            /**/
            /*******************************************计算列*************************/

            System.Data.DataColumn column = new DataColumn("exp1", typeof(float));
            table.Columns.Add(column);


            //简单计算
            column.Expression = "value*2";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=2;

            //字符串函数
            column.Expression = "len(id)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=3;

            //字符串函数
            column.Expression = "len(' '+id+' ')";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=5;

            //字符串函数
            column.Expression = "len(trim(' '+id+' '))";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=3;

            //字符串函数
            column.Expression = "substring(id,3,len(id)-2)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=1; //substring的起始字符位置为1不是0

            //类型转换
            column.Expression = "convert(substring(id,3,len(id)-2),'System.Int32')*1.6";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=1.6;

            //相当于sqlserver的isnull
            column.Expression = "isnull(value,10)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=1;

            //三元运算符,相当于sqlserver的case when
            column.Expression = "iif(value>5,1000,2000)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=2000;

            //like运算符
            column.Expression = "iif(id like '%1',1000,2000)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=1000;

            //in运算符
            column.Expression = "iif(id not in('id1'),1000,2000)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=2000;

            //嵌套的三元运算
            column.Expression = "iif(value>5,1000,iif(id like '%1',4000,2000))";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=4000;


            //客户端计算所占总数的百分比
            column.Expression = "value/sum(value)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=0.01818182


            //客户端计算差值,比如nba常规赛的胜场差
            column.Expression = "max(value)-value";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=9


            //***********************父子表计算*************************************/


            //初始化子表,父子表关系

            DataTable tableChild = new DataTable();

            tableChild.Columns.Add("id", typeof(string));
            tableChild.Columns.Add("value", typeof(int));

            System.Data.DataSet ds = new DataSet();
            ds.Tables.Add(tableChild);
            ds.Tables.Add(table);
            DataRelation relation = new DataRelation("relation", table.Columns["id"], tableChild.Columns["id"]);
            ds.Relations.Add(relation);

            for (int i = 1; i <= 10; i++)
            {
                System.Data.DataRow dRow = tableChild.NewRow();
                dRow["id"] = "id1";
                dRow["value"] = i;
                tableChild.Rows.Add(dRow);
            }


            //计算子表记录数
            column.Expression = "count(child(relation).value)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=10;

 

            //计算父子表的百分比
            column.Expression = "value/sum(child(relation).value)";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=0.01818182;


            //计算父子表的差值,比如父表为库存数量,子表为订购数量,计算得出需要补充的数量
            column.Expression = "iif(value-sum(child(relation).value)>0,0,value-sum(child(relation).value))";
            test = table.Select("id='id1'")[0]["exp1"];
            Console.WriteLine(test);
            //test=-54;

            //比较遗憾的是没有发现能够计算同比和环比的方法,而且计算列无法作为约束
            //************结束,DataTable可以让你尽量发挥聪明才智来减少繁杂的sql语句并且减轻服务器计算符合^&^

        }
    }
}

DataTable求平均值,最大值,最小值

DataTable  dsValue;
if  (dsValue.Rows.Count > 0)
            {
                maxValue =  float .Parse(dsValue.Compute( "max(["  + ParaName +  "])" ,  ""  ).ToString());
                minValue =  float .Parse(dsValue.Compute( "min(["  + ParaName +  "])" ,  ""  ).ToString());
                avgValue =  float .Parse(dsValue.Compute( "avg(["  + ParaName +  "])" ,  ""  ).ToString());
                SDAVG =  float .Parse(dsValue.Compute( "avg(["  + SDVALUE +  "])" ,  ""  ).ToString()); // 设定平均
               
                 if  (avgValue.ToString() !=  string .Empty && SDAVG.ToString() !=  string .Empty)
                {
                    sszavg_sdzavg_subValue =  Math .Abs(SDAVG - avgValue);
                }
                 try
                {
                    sdValue =  float .Parse(dsValue.Compute( "StDev(["  + ParaName +  "])" ,  ""  ).ToString());
                }
                 catch  ( Exception  ex)
                {
                    sdValue = 0;
                }
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值