Compute函数的参数就两个:Expression,和Filter。
Expresstion是计算表达式,关于Expression的详细内容请看这里“http://msdn2.microsoft.com/zh-cn/library/system.data.datacolumn.expression(VS.80).aspx”。而Filter则是条件过滤器,类似sql的Where条件。
- DataTable dt = new DataTable();
- object obj = dt.Compute("iif(1000=5,1000,iif(100>100,4001,2000))", null);
- Response.Write(obj);
-
-
- 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(Convert.ToDecimal(a));
-
- test = table.Compute("1+1", "false");
- Console.WriteLine(test);
-
- test = table.Compute("abs(1)", "");
- Console.WriteLine(test);
-
- test = table.Compute("2%2", "");
- Console.WriteLine(test);
-
-
-
- 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("count(id)", "false");
- Console.WriteLine(test);
-
- test = table.Compute("count(id)", "true");
- Console.WriteLine(test);
-
-
-
- test = table.Compute("sum(value)", "");
- Console.WriteLine(test);
-
-
-
- test = table.Compute("avg(value)", "");
- Console.WriteLine(test);
-
-
- test = table.Compute("min(value)", "");
- Console.WriteLine(test);
-
- test = table.Compute("max(value)", "");
- Console.WriteLine(test);
-
- test = table.Compute("StDev(value)", "");
- Console.WriteLine(test);
-
- test = table.Compute("Var(value)", "");
- Console.WriteLine(test);
-
-
- test = table.Compute("max(value)/sum(value)", "");
- Console.WriteLine(test);
-
-
- 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);
-
- column.Expression = "len(id)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "len(‘ ‘+id+‘ ‘)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "len(trim(‘ ‘+id+‘ ‘))";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "substring(id,3,len(id)-2)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "convert(substring(id,3,len(id)-2),‘System.Int32‘)*1.6";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "isnull(value,10)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "iif(value>5,1000,2000)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "iif(id like ‘%1‘,1000,2000)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "iif(id not in(‘id1‘),1000,2000)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
- column.Expression = "iif(value>5,1000,iif(id like ‘%1‘,4000,2000))";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
-
- column.Expression = "value/sum(value)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
-
- column.Expression = "max(value)-value";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
-
-
-
- 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);
-
-
-
- column.Expression = "value/sum(child(relation).value)";
- test = table.Select("id=‘id1‘")[0]["exp1"];
- Console.WriteLine(test);
-
-
- 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);
-
<转> C#中DataTable中的Compute方法使用收集
原文:http://www.cnblogs.com/yuneipanguan/p/4080117.html