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,效能極佳。// 等效但較冗長
decimal sumPrice = DbSession.Default.From()
.Where(Products._.CategoryID == 2)
.Select(Products._.UnitPrice.Sum())
.ToScalar();
統計方法為常見聚合提供**語意更清晰、程式碼更短**的捷徑。
DbSession 統計方法是 Hxj.Data 為高頻聚合查詢提供的貼心語法糖,零學習成本、效能無損耗。