2011年7月10日 星期日

C# 取TABLE中的最大最小值~

以前,為了要取最大最小值,都要在那裡寫一個LOOP,玩比大小,最後取得最大或最小值。
(或是一些運算式,都會傻傻的,土法煉鋼)

最近因為瘋狂拜古狗大師,才發現原來有內建的FUNCTION可以用。@_@



DataTable本身有個Compute的function,可以 拿來使用。
代入2個參數,第一個是運算式,第二個是條件。

如要取id最大值。
function string getMax(DataTable dt)
{
            Return Convert.ToString(dt.Compute("MAX(id)", ""));
}


Available aggregate functions include those shown in the following table.

Avg() The average of values in a column
Count() The number of rows (values) in a column
Max() The largest value in a column
Min() The smallest value in a column
StDev() The standard deviation of values in a column
Sum() The sum of values in a column
Var() The statistical variance of values in a column

DataTable.Compute method http://msdn.microsoft.com/en-us/library/system.data.datatable.compute.aspx

沒有留言: