DataTable Select

http://www.dotblogs.com.tw/joysdw12/archive/2010/11/09/19294.aspx

public void TDataTableSelect ()
{
// 建立 測試 DataTable
DataTable table = new DataTable ();
table.Columns.Add (“key”, typeof (string));
table.Columns.Add (“valor”, typeof (int));
table.Columns.Add (“data”, typeof (DateTime));
DataRow dr;
para (int i = 0; i <5; i ++)
{
dr = tabela.NovaLinha ();
dr [“chave”] = “chave” + i;
dr [“valor”] = i * 100;
dr [“data”] = DateTime.Now.AddYears (i) .Date;
table.Rows.Add (dr);
}

/******************************************
* 常數計算 DataTable.Compute 方法

* 參數:

* expression:要計算的運算式。

* filter:要限制在運算式中評估之資料列的篩選條件。

* ****************************************/


// 加總運算
object testVar = table.Compute("1 + 1", "");
Response.Write("加總運算:" + testVar.ToString() + "

“); // resultado = 2;

// 加總運算,無filter
testVar
= table.Compute("1 + 1", "false");
Response.Write("加總運算,無filter:" + testVar.ToString() + "

“); // resultado = 2;

// 絕對值運算
testVar
= table.Compute("abs(1)", "");
Response.Write("絕對值運算:" + testVar.ToString() + "

“); // resultado = nulo;

// 求餘數運算
testVar
= table.Compute(" 2%2 ", "");
Response.Write("求餘數運算" + testVar.ToString() + "

“); // resultado = 0;

// 加法運算,exception: 無法評估。運算式 'System.Data.NameNode' 不是彙總。
//testVar = table.Compute("value+1", "true");
//Response.Write(testVar.ToString() + "

“);

/******************************************
* 彙總計算 DataTable.Compute 方法

* 參數:

* expression:要計算的運算式。

* filter:要限制在運算式中評估之資料列的篩選條件。

* ****************************************/


// 筆數計算
testVar
= table.Compute("count(key)", "");
Response.Write("筆數計算:" + testVar.ToString() + "

“); // resultado = 3;

// 筆數計算,有filter
testVar
= table.Compute("count(key)", "key='key1'");
Response.Write("筆數計算,有filter:" + testVar.ToString() + "

“); // resultado = 1;

// 欄位總和計算
testVar
= table.Compute("sum(value)", "");
Response.Write("欄位總和計算:" + testVar.ToString() + "

“); // resultado = 1000;

// 欄位平均計算
testVar
= table.Compute("avg(value)", "");
Response.Write("欄位平均計算:" + testVar.ToString() + "

“); // resultado = 200;

// 找出最小值
testVar
= table.Compute("min(value)", "");
Response.Write("找出最小值:" + testVar.ToString() + "

“); // resultado = 0;

// 找出最大值
testVar
= table.Compute("max(value)", "");
Response.Write("找出最大值:" + testVar.ToString() + "

“); // resultado = 400;

// 統計標準偏差
testVar
= table.Compute("StDev(value)", "");
Response.Write("統計標準偏差:" + testVar.ToString() + "

“); // resultado = 158.113883008419;

// 統計方差
testVar
= table.Compute("Var(value)", "");
Response.Write("統計方差:" + testVar.ToString() + "

“); // resultado = 25000;

// 複雜計算
testVar
= table.Compute("max(value) / sum(value)", "");
Response.Write("複雜計算:" + testVar.ToString() + "

“); // resultado = 0,4;

/******************************************
* 欄位計算 DataColumn.Expression 屬性

* 屬性值:

* 運算式,用來計算資料行的值或建立彙總資料行。運算式的傳回型別是由資料行的 DataType 所判斷。

* ****************************************/


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

// 乘法計算
column
.Expression = "value*2";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("乘法計算:" + testVar.ToString() + "

“); // resultado = 200;

// 字串長度計算函數
column
.Expression = "len(key)";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("字串長度計算函數:" + testVar.ToString() + "

“); // resultado = 4;

// 含空白字串長度計算函數
column
.Expression = "len(' '+key+' ') ";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("含空白字串長度計算函數:" + testVar.ToString() + "

“); // resultado = 6;

// 去空白後字串長度計算函數
column
.Expression = "len(trim(' '+key+' '))";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("去空白後字串長度計算函數:" + testVar.ToString() + "

“); // resultado = 4;

// 切字串函數 (SQL的Substring起始Index為1)
column
.Expression = "substring(key,4,len(key)-3)";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("切字串函數:" + testVar.ToString() + "

“); // resultado = 1;

// 資料型別轉換
column
.Expression = "convert(substring(key,4,len(key)-3),'System.Int32')*1.6";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("資料型別轉換:" + testVar.ToString() + "

“); // resultado = 1,6;

// SQL-ISNULL
column
.Expression = "isnull(value,10)";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("SQL-ISNULL:" + testVar.ToString() + "

“); // resultado = 100;

// CASE運算式
column
.Expression = "iif(value>5,1000,2000)";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("CASE運算式" + testVar.ToString() + "

“); // resultado = 1000;

// LINK運算式
column
.Expression = "iif(key like '%1',1000,2000)";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("LINK運算式:" + testVar.ToString() + "

“); // resultado = 1000;

// IN運算式
column
.Expression = "iif(key not in('key1'),1000,2000)";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("IN運算式:" + testVar.ToString() + "

“); // resultado = 2000;

// 多層三元運算
column
.Expression = "iif(value>5,1000,iif(key like '%1',4000,2000))";
testVar
= table.Select("key='key1'")[0]["exp1"];
Response.Write("多層三元運算:" + testVar.ToString() + "

“); // resultado = 1000;

/******************************************
* 資料查詢 DataTable.Select 方法

* 參數:

* filterExpression:用來篩選資料列的準則。

* sort:指定資料行和排序方向的字串。

* ****************************************/


DataRow[] row;

// 基本查詢
testVar
= table.Select("key='key0'")[0]["key"].ToString();
Response.Write("基本查詢:" + testVar.ToString() + "

“); // resultado = chave0;

// 多條件查詢
testVar
= table.Select("key='key1' and value=100")[0]["key"].ToString();
Response.Write("多條件查詢:" + testVar.ToString() + "

“); // resultado = chave1;

// DateTime日期查詢
string nowDate = DateTime.Now.ToString("#yyyy-MM-dd#");
testVar
= table.Select("date=" + nowDate + "")[0]["key"].ToString();
Response.Write("日期查詢:" + testVar.ToString() + "

“); // resultado = chave0;
}