首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

Hxj.Data DbSession 輔助統計方法:Sum/Avg/Count/Max/Min 一鍵完成聚合查詢

     編輯:關於ASP.NET

DbSession 直達統計方法,無需 From 查詢鏈

方法簽名

Sum(Field field, WhereClip where)
Avg(Field field, WhereClip where)
Count(Field field, WhereClip where) // field 通常為 Field.All
Max(Field field, WhereClip where)
Min(Field field, WhereClip where)

使用範例

// CategoryID=2 的產品單價合計
decimal sumPrice = DbSession.Default.Sum(Products._.UnitPrice, Products._.CategoryID == 2);

// 平均單價
decimal avgPrice = DbSession.Default.Avg(Products._.UnitPrice, Products._.CategoryID == 2);

// 記錄筆數
int count = DbSession.Default.Count(Field.All, Products._.CategoryID == 2);

// 最高/最低單價
decimal maxPrice = DbSession.Default.Max(Products._.UnitPrice, Products._.CategoryID == 2);
decimal minPrice = DbSession.Default.Min(Products._.UnitPrice, Products._.CategoryID == 2);

回傳值處理

  • 結果為 NULL(如無符合記錄)時,數值型別回傳 0,字串/參考型別回傳 null。
  • 內部自動執行 SELECT SUM(...) 等純量 SQL,效能極佳。

對比:From 查詢鏈寫法

// 等效但較冗長
decimal sumPrice = DbSession.Default.From()
    .Where(Products._.CategoryID == 2)
    .Select(Products._.UnitPrice.Sum())
    .ToScalar();

統計方法為常見聚合提供**語意更清晰、程式碼更短**的捷徑。

適用場景

  • 儀表板關鍵指標(KPI)即時查詢。
  • 報表彙總列(總計/平均/筆數)。
  • 業務規則驗證(如:庫存總量是否足夠)。

DbSession 統計方法是 Hxj.Data 為高頻聚合查詢提供的貼心語法糖,零學習成本、效能無損耗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved