From 5018a1158e9c1051c0157966d729cb0aa15f2d0b Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 8 Jul 2017 02:32:32 +0800 Subject: [PATCH] AVG, SUM, MAX, MIN Overload --- Src/Asp.Net/SqlServerTest/Demos/1_Query.cs | 1 + .../QueryableProvider/QueryableProvider.cs | 176 ++++++++++++++++-- Src/Asp.Net/SqlSugar/Interface/IQueryable.cs | 49 +++++ 3 files changed, 213 insertions(+), 13 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs b/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs index c302adc6c..92e9f5966 100644 --- a/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs +++ b/Src/Asp.Net/SqlServerTest/Demos/1_Query.cs @@ -162,6 +162,7 @@ namespace OrmTest.Demo var getByWhere = db.Queryable().Where(it => it.Id == 1 || it.Name == "a").ToList(); var getByFuns = db.Queryable().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList(); var sum = db.Queryable().Sum(it => it.Id); + var sum2 = db.Queryable((st,sc)=>st.SchoolId==sc.Id).Sum((st,sc) => sc.Id); var isAny = db.Queryable().Where(it => it.Id == -1).Any(); var isAny2 = db.Queryable().Any(it => it.Id == -1); var getListByRename = db.Queryable().AS("Student").ToList(); diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 3b2145646..d6e276b0e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -405,10 +405,9 @@ namespace SqlSugar } public virtual TResult Max(Expression> expression) { - var isSingle = QueryBuilder.IsSingle(); - var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); - return Max(lamResult.GetResultString()); + return _Max(expression); } + public virtual TResult Min(string minField) { this.Select(string.Format(QueryBuilder.MinTemplate, minField)); @@ -417,10 +416,9 @@ namespace SqlSugar } public virtual TResult Min(Expression> expression) { - var isSingle = QueryBuilder.IsSingle(); - var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); - return Min(lamResult.GetResultString()); + return _Min(expression); } + public virtual TResult Sum(string sumField) { this.Select(string.Format(QueryBuilder.SumTemplate, sumField)); @@ -429,10 +427,9 @@ namespace SqlSugar } public virtual TResult Sum(Expression> expression) { - var isSingle = QueryBuilder.IsSingle(); - var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); - return Sum(lamResult.GetResultString()); + return _Sum(expression); } + public virtual TResult Avg(string avgField) { this.Select(string.Format(QueryBuilder.AvgTemplate, avgField)); @@ -441,11 +438,8 @@ namespace SqlSugar } public virtual TResult Avg(Expression> expression) { - var isSingle = QueryBuilder.IsSingle(); - var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); - return Avg(lamResult.GetResultString()); + return _Avg(expression); } - public virtual string ToJson() { return this.Context.RewritableMethods.SerializeObject(this.ToList()); @@ -552,6 +546,30 @@ namespace SqlSugar GroupBy(result); return this; } + protected TResult _Min(Expression expression) + { + var isSingle = QueryBuilder.IsSingle(); + var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); + return Min(lamResult.GetResultString()); + } + protected TResult _Avg(Expression expression) + { + var isSingle = QueryBuilder.IsSingle(); + var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); + return Avg(lamResult.GetResultString()); + } + protected TResult _Max(Expression expression) + { + var isSingle = QueryBuilder.IsSingle(); + var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); + return Max(lamResult.GetResultString()); + } + protected TResult _Sum(Expression expression) + { + var isSingle = QueryBuilder.IsSingle(); + var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple); + return Sum(lamResult.GetResultString()); + } public ISugarQueryable _PartitionBy(Expression expression) { LambdaExpression lambda = expression as LambdaExpression; @@ -729,6 +747,25 @@ namespace SqlSugar return this; } #endregion + + #region Aggr + public TResult Max(Expression> expression) + { + return _Max(expression); + } + public TResult Min(Expression> expression) + { + return _Min(expression); + } + public TResult Sum(Expression> expression) + { + return _Sum(expression); + } + public TResult Avg(Expression> expression) + { + return _Avg(expression); + } + #endregion } #endregion #region T3 @@ -838,6 +875,24 @@ namespace SqlSugar } #endregion + #region Aggr + public TResult Max(Expression> expression) + { + return _Max(expression); + } + public TResult Min(Expression> expression) + { + return _Min(expression); + } + public TResult Sum(Expression> expression) + { + return _Sum(expression); + } + public TResult Avg(Expression> expression) + { + return _Avg(expression); + } + #endregion } #endregion #region T4 @@ -967,6 +1022,25 @@ namespace SqlSugar return this; } #endregion + + #region Aggr + public TResult Max(Expression> expression) + { + return _Max(expression); + } + public TResult Min(Expression> expression) + { + return _Min(expression); + } + public TResult Sum(Expression> expression) + { + return _Sum(expression); + } + public TResult Avg(Expression> expression) + { + return _Avg(expression); + } + #endregion } #endregion #region T5 @@ -1122,6 +1196,25 @@ namespace SqlSugar return this; } #endregion + + #region Aggr + public TResult Max(Expression> expression) + { + return _Max(expression); + } + public TResult Min(Expression> expression) + { + return _Min(expression); + } + public TResult Sum(Expression> expression) + { + return _Sum(expression); + } + public TResult Avg(Expression> expression) + { + return _Avg(expression); + } + #endregion } #endregion #region T6 @@ -1303,6 +1396,25 @@ namespace SqlSugar return this; } #endregion + + #region Aggr + public TResult Max(Expression> expression) + { + return _Max(expression); + } + public TResult Min(Expression> expression) + { + return _Min(expression); + } + public TResult Sum(Expression> expression) + { + return _Sum(expression); + } + public TResult Avg(Expression> expression) + { + return _Avg(expression); + } + #endregion } #endregion #region T7 @@ -1511,6 +1623,25 @@ namespace SqlSugar } #endregion + + #region Aggr + public TResult Max(Expression> expression) + { + return _Max(expression); + } + public TResult Min(Expression> expression) + { + return _Min(expression); + } + public TResult Sum(Expression> expression) + { + return _Sum(expression); + } + public TResult Avg(Expression> expression) + { + return _Avg(expression); + } + #endregion } #endregion #region T8 @@ -1745,6 +1876,25 @@ namespace SqlSugar } #endregion + + #region Aggr + public TResult Max(Expression> expression) + { + return _Max(expression); + } + public TResult Min(Expression> expression) + { + return _Min(expression); + } + public TResult Sum(Expression> expression) + { + return _Sum(expression); + } + public TResult Avg(Expression> expression) + { + return _Avg(expression); + } + #endregion } #endregion } diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index 5afc40d3c..0ccbe2f5f 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -117,6 +117,13 @@ namespace SqlSugar new ISugarQueryable GroupBy(Expression> expression); ISugarQueryable GroupBy(Expression> expression); #endregion + + #region Aggr + TResult Max(Expression> expression); + TResult Min(Expression> expression); + TResult Sum(Expression> expression); + TResult Avg(Expression> expression); + #endregion } public partial interface ISugarQueryable : ISugarQueryable { @@ -149,6 +156,13 @@ namespace SqlSugar ISugarQueryable GroupBy(Expression> expression); ISugarQueryable GroupBy(Expression> expression); #endregion + + #region Aggr + TResult Max(Expression> expression); + TResult Min(Expression> expression); + TResult Sum(Expression> expression); + TResult Avg(Expression> expression); + #endregion } public partial interface ISugarQueryable : ISugarQueryable { @@ -186,6 +200,13 @@ namespace SqlSugar ISugarQueryable GroupBy(Expression> expression); ISugarQueryable GroupBy(Expression> expression); #endregion + + #region Aggr + TResult Max(Expression> expression); + TResult Min(Expression> expression); + TResult Sum(Expression> expression); + TResult Avg(Expression> expression); + #endregion } public partial interface ISugarQueryable : ISugarQueryable { @@ -229,6 +250,13 @@ namespace SqlSugar ISugarQueryable GroupBy(Expression> expression); ISugarQueryable GroupBy(Expression> expression); #endregion + + #region Aggr + TResult Max(Expression> expression); + TResult Min(Expression> expression); + TResult Sum(Expression> expression); + TResult Avg(Expression> expression); + #endregion } public partial interface ISugarQueryable : ISugarQueryable { @@ -276,6 +304,13 @@ namespace SqlSugar ISugarQueryable GroupBy(Expression> expression); ISugarQueryable GroupBy(Expression> expression); #endregion + + #region Aggr + TResult Max(Expression> expression); + TResult Min(Expression> expression); + TResult Sum(Expression> expression); + TResult Avg(Expression> expression); + #endregion } public partial interface ISugarQueryable : ISugarQueryable { @@ -328,6 +363,13 @@ namespace SqlSugar ISugarQueryable GroupBy(Expression> expression); ISugarQueryable GroupBy(Expression> expression); #endregion + + #region Aggr + TResult Max(Expression> expression); + TResult Min(Expression> expression); + TResult Sum(Expression> expression); + TResult Avg(Expression> expression); + #endregion } public partial interface ISugarQueryable : ISugarQueryable { @@ -385,6 +427,13 @@ namespace SqlSugar ISugarQueryable GroupBy(Expression> expression); ISugarQueryable GroupBy(Expression> expression); #endregion + + #region Aggr + TResult Max(Expression> expression); + TResult Min(Expression> expression); + TResult Sum(Expression> expression); + TResult Avg(Expression> expression); + #endregion } }