程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# DataTable中Compute辦法用法集錦(數值/字符串/運算符/表等操作)

C# DataTable中Compute辦法用法集錦(數值/字符串/運算符/表等操作)

編輯:C#入門知識

C# DataTable中Compute辦法用法集錦(數值/字符串/運算符/表等操作)。本站提示廣大學習愛好者:(C# DataTable中Compute辦法用法集錦(數值/字符串/運算符/表等操作))文章只能為提供參考,不一定能成為您想要的結果。以下是C# DataTable中Compute辦法用法集錦(數值/字符串/運算符/表等操作)正文


本文實例講述了C# DataTable中Compute辦法用法。分享給年夜家供年夜家參考,詳細以下:

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=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語句而且加重辦事器盤算相符

願望本文所述對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved