摘要:(200-08-14) 使用LINQ 做 數值陣列 查詢
命名空間:System.Linq;
LINEQ可只接使用擴充Method 只接對陣列做出查詢,但必需加入System.Linq 的命名空間
程式碼:
//使用Linq做數值陣列查詢
Int32[] _number = new Int32[] { 5,28,10,5,100,34,6,48};
Int32 max=_number.AsEnumerable().Max(); //取最大值
Int32 _max = _number.Max(); //只接取得最大值
Int32 min = _number.AsEnumerable().Min(); //取最小值
Int32 avg = (Int32)_number.AsEnumerable().Average(); //最平均值
Int32 sum = _number.AsEnumerable().Sum(); //全部加總
Array.Sort(_number); //數值排序
//輸出
System.Console.WriteLine(String.Format("最大值:{0}",max));
System.Console.WriteLine(String.Format("最小值:{0}",min));
System.Console.WriteLine(String.Format("平均值:{0}", avg));
System.Console.WriteLine(String.Format("加總:{0}", sum));