Browse Source

AVG, SUM, MAX, MIN Overload

pull/12/MERGE
sunkaixuan 7 years ago
parent
commit
5018a1158e
  1. 1
      Src/Asp.Net/SqlServerTest/Demos/1_Query.cs
  2. 176
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  3. 49
      Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

1
Src/Asp.Net/SqlServerTest/Demos/1_Query.cs

@ -162,6 +162,7 @@ namespace OrmTest.Demo
var getByWhere = db.Queryable<Student>().Where(it => it.Id == 1 || it.Name == "a").ToList();
var getByFuns = db.Queryable<Student>().Where(it => SqlFunc.IsNullOrEmpty(it.Name)).ToList();
var sum = db.Queryable<Student>().Sum(it => it.Id);
var sum2 = db.Queryable<Student,School>((st,sc)=>st.SchoolId==sc.Id).Sum((st,sc) => sc.Id);
var isAny = db.Queryable<Student>().Where(it => it.Id == -1).Any();
var isAny2 = db.Queryable<Student>().Any(it => it.Id == -1);
var getListByRename = db.Queryable<School>().AS("Student").ToList();

176
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -405,10 +405,9 @@ namespace SqlSugar
}
public virtual TResult Max<TResult>(Expression<Func<T, TResult>> expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
return Max<TResult>(lamResult.GetResultString());
return _Max<TResult>(expression);
}
public virtual TResult Min<TResult>(string minField)
{
this.Select(string.Format(QueryBuilder.MinTemplate, minField));
@ -417,10 +416,9 @@ namespace SqlSugar
}
public virtual TResult Min<TResult>(Expression<Func<T, TResult>> expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
return Min<TResult>(lamResult.GetResultString());
return _Min<TResult>(expression);
}
public virtual TResult Sum<TResult>(string sumField)
{
this.Select(string.Format(QueryBuilder.SumTemplate, sumField));
@ -429,10 +427,9 @@ namespace SqlSugar
}
public virtual TResult Sum<TResult>(Expression<Func<T, TResult>> expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
return Sum<TResult>(lamResult.GetResultString());
return _Sum<TResult>(expression);
}
public virtual TResult Avg<TResult>(string avgField)
{
this.Select(string.Format(QueryBuilder.AvgTemplate, avgField));
@ -441,11 +438,8 @@ namespace SqlSugar
}
public virtual TResult Avg<TResult>(Expression<Func<T, TResult>> expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
return Avg<TResult>(lamResult.GetResultString());
return _Avg<TResult>(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<TResult>(Expression expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
return Min<TResult>(lamResult.GetResultString());
}
protected TResult _Avg<TResult>(Expression expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
return Avg<TResult>(lamResult.GetResultString());
}
protected TResult _Max<TResult>(Expression expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
return Max<TResult>(lamResult.GetResultString());
}
protected TResult _Sum<TResult>(Expression expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple);
return Sum<TResult>(lamResult.GetResultString());
}
public ISugarQueryable<T> _PartitionBy(Expression expression)
{
LambdaExpression lambda = expression as LambdaExpression;
@ -729,6 +747,25 @@ namespace SqlSugar
return this;
}
#endregion
#region Aggr
public TResult Max<TResult>(Expression<Func<T, T2, TResult>> expression)
{
return _Max<TResult>(expression);
}
public TResult Min<TResult>(Expression<Func<T, T2, TResult>> expression)
{
return _Min<TResult>(expression);
}
public TResult Sum<TResult>(Expression<Func<T, T2, TResult>> expression)
{
return _Sum<TResult>(expression);
}
public TResult Avg<TResult>(Expression<Func<T, T2, TResult>> expression)
{
return _Avg<TResult>(expression);
}
#endregion
}
#endregion
#region T3
@ -838,6 +875,24 @@ namespace SqlSugar
}
#endregion
#region Aggr
public TResult Max<TResult>(Expression<Func<T, T2,T3, TResult>> expression)
{
return _Max<TResult>(expression);
}
public TResult Min<TResult>(Expression<Func<T, T2,T3, TResult>> expression)
{
return _Min<TResult>(expression);
}
public TResult Sum<TResult>(Expression<Func<T, T2,T3, TResult>> expression)
{
return _Sum<TResult>(expression);
}
public TResult Avg<TResult>(Expression<Func<T, T2,T3, TResult>> expression)
{
return _Avg<TResult>(expression);
}
#endregion
}
#endregion
#region T4
@ -967,6 +1022,25 @@ namespace SqlSugar
return this;
}
#endregion
#region Aggr
public TResult Max<TResult>(Expression<Func<T, T2, T3,T4, TResult>> expression)
{
return _Max<TResult>(expression);
}
public TResult Min<TResult>(Expression<Func<T, T2, T3,T4, TResult>> expression)
{
return _Min<TResult>(expression);
}
public TResult Sum<TResult>(Expression<Func<T, T2, T3,T4, TResult>> expression)
{
return _Sum<TResult>(expression);
}
public TResult Avg<TResult>(Expression<Func<T, T2, T3,T4, TResult>> expression)
{
return _Avg<TResult>(expression);
}
#endregion
}
#endregion
#region T5
@ -1122,6 +1196,25 @@ namespace SqlSugar
return this;
}
#endregion
#region Aggr
public TResult Max<TResult>(Expression<Func<T, T2, T3, T4,T5, TResult>> expression)
{
return _Max<TResult>(expression);
}
public TResult Min<TResult>(Expression<Func<T, T2, T3, T4,T5, TResult>> expression)
{
return _Min<TResult>(expression);
}
public TResult Sum<TResult>(Expression<Func<T, T2, T3, T4,T5, TResult>> expression)
{
return _Sum<TResult>(expression);
}
public TResult Avg<TResult>(Expression<Func<T, T2, T3, T4,T5, TResult>> expression)
{
return _Avg<TResult>(expression);
}
#endregion
}
#endregion
#region T6
@ -1303,6 +1396,25 @@ namespace SqlSugar
return this;
}
#endregion
#region Aggr
public TResult Max<TResult>(Expression<Func<T, T2, T3, T4, T5,T6, TResult>> expression)
{
return _Max<TResult>(expression);
}
public TResult Min<TResult>(Expression<Func<T, T2, T3, T4, T5,T6, TResult>> expression)
{
return _Min<TResult>(expression);
}
public TResult Sum<TResult>(Expression<Func<T, T2, T3, T4, T5,T6, TResult>> expression)
{
return _Sum<TResult>(expression);
}
public TResult Avg<TResult>(Expression<Func<T, T2, T3, T4, T5,T6, TResult>> expression)
{
return _Avg<TResult>(expression);
}
#endregion
}
#endregion
#region T7
@ -1511,6 +1623,25 @@ namespace SqlSugar
}
#endregion
#region Aggr
public TResult Max<TResult>(Expression<Func<T, T2, T3, T4, T5, T6,T7, TResult>> expression)
{
return _Max<TResult>(expression);
}
public TResult Min<TResult>(Expression<Func<T, T2, T3, T4, T5, T6,T7, TResult>> expression)
{
return _Min<TResult>(expression);
}
public TResult Sum<TResult>(Expression<Func<T, T2, T3, T4, T5, T6,T7, TResult>> expression)
{
return _Sum<TResult>(expression);
}
public TResult Avg<TResult>(Expression<Func<T, T2, T3, T4, T5, T6,T7, TResult>> expression)
{
return _Avg<TResult>(expression);
}
#endregion
}
#endregion
#region T8
@ -1745,6 +1876,25 @@ namespace SqlSugar
}
#endregion
#region Aggr
public TResult Max<TResult>(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, TResult>> expression)
{
return _Max<TResult>(expression);
}
public TResult Min<TResult>(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, TResult>> expression)
{
return _Min<TResult>(expression);
}
public TResult Sum<TResult>(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, TResult>> expression)
{
return _Sum<TResult>(expression);
}
public TResult Avg<TResult>(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, TResult>> expression)
{
return _Avg<TResult>(expression);
}
#endregion
}
#endregion
}

49
Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

@ -117,6 +117,13 @@ namespace SqlSugar
new ISugarQueryable<T, T2> GroupBy(Expression<Func<T, object>> expression);
ISugarQueryable<T, T2> GroupBy(Expression<Func<T, T2, object>> expression);
#endregion
#region Aggr
TResult Max<TResult>(Expression<Func<T,T2, TResult>> expression);
TResult Min<TResult>(Expression<Func<T,T2, TResult>> expression);
TResult Sum<TResult>(Expression<Func<T,T2, TResult>> expression);
TResult Avg<TResult>(Expression<Func<T,T2, TResult>> expression);
#endregion
}
public partial interface ISugarQueryable<T, T2, T3> : ISugarQueryable<T>
{
@ -149,6 +156,13 @@ namespace SqlSugar
ISugarQueryable<T, T2, T3> GroupBy(Expression<Func<T, T2, object>> expression);
ISugarQueryable<T, T2, T3> GroupBy(Expression<Func<T, T2, T3, object>> expression);
#endregion
#region Aggr
TResult Max<TResult>(Expression<Func<T, T2,T3, TResult>> expression);
TResult Min<TResult>(Expression<Func<T, T2,T3, TResult>> expression);
TResult Sum<TResult>(Expression<Func<T, T2,T3, TResult>> expression);
TResult Avg<TResult>(Expression<Func<T, T2,T3, TResult>> expression);
#endregion
}
public partial interface ISugarQueryable<T, T2, T3, T4> : ISugarQueryable<T>
{
@ -186,6 +200,13 @@ namespace SqlSugar
ISugarQueryable<T, T2, T3, T4> GroupBy(Expression<Func<T, T2, T3, object>> expression);
ISugarQueryable<T, T2, T3, T4> GroupBy(Expression<Func<T, T2, T3, T4, object>> expression);
#endregion
#region Aggr
TResult Max<TResult>(Expression<Func<T, T2, T3,T4,TResult>> expression);
TResult Min<TResult>(Expression<Func<T, T2, T3,T4,TResult>> expression);
TResult Sum<TResult>(Expression<Func<T, T2, T3,T4,TResult>> expression);
TResult Avg<TResult>(Expression<Func<T, T2, T3,T4,TResult>> expression);
#endregion
}
public partial interface ISugarQueryable<T, T2, T3, T4, T5> : ISugarQueryable<T>
{
@ -229,6 +250,13 @@ namespace SqlSugar
ISugarQueryable<T, T2, T3, T4, T5> GroupBy(Expression<Func<T, T2, T3, T4, object>> expression);
ISugarQueryable<T, T2, T3, T4, T5> GroupBy(Expression<Func<T, T2, T3, T4, T5, object>> expression);
#endregion
#region Aggr
TResult Max<TResult>(Expression<Func<T, T2, T3, T4,T5,TResult>> expression);
TResult Min<TResult>(Expression<Func<T, T2, T3, T4,T5,TResult>> expression);
TResult Sum<TResult>(Expression<Func<T, T2, T3, T4,T5,TResult>> expression);
TResult Avg<TResult>(Expression<Func<T, T2, T3, T4,T5,TResult>> expression);
#endregion
}
public partial interface ISugarQueryable<T, T2, T3, T4, T5, T6> : ISugarQueryable<T>
{
@ -276,6 +304,13 @@ namespace SqlSugar
ISugarQueryable<T, T2, T3, T4, T5, T6> GroupBy(Expression<Func<T, T2, T3, T4, T5, object>> expression);
ISugarQueryable<T, T2, T3, T4, T5, T6> GroupBy(Expression<Func<T, T2, T3, T4, T5, T6, object>> expression);
#endregion
#region Aggr
TResult Max<TResult>(Expression<Func<T, T2, T3, T4, T5,T6,TResult>> expression);
TResult Min<TResult>(Expression<Func<T, T2, T3, T4, T5,T6,TResult>> expression);
TResult Sum<TResult>(Expression<Func<T, T2, T3, T4, T5,T6,TResult>> expression);
TResult Avg<TResult>(Expression<Func<T, T2, T3, T4, T5,T6,TResult>> expression);
#endregion
}
public partial interface ISugarQueryable<T, T2, T3, T4, T5, T6, T7> : ISugarQueryable<T>
{
@ -328,6 +363,13 @@ namespace SqlSugar
ISugarQueryable<T, T2, T3, T4, T5, T6, T7> GroupBy(Expression<Func<T, T2, T3, T4, T5, T6, object>> expression);
ISugarQueryable<T, T2, T3, T4, T5, T6, T7> GroupBy(Expression<Func<T, T2, T3, T4, T5, T6, T7, object>> expression);
#endregion
#region Aggr
TResult Max<TResult>(Expression<Func<T, T2, T3, T4, T5, T6,T7,TResult>> expression);
TResult Min<TResult>(Expression<Func<T, T2, T3, T4, T5, T6,T7,TResult>> expression);
TResult Sum<TResult>(Expression<Func<T, T2, T3, T4, T5, T6,T7,TResult>> expression);
TResult Avg<TResult>(Expression<Func<T, T2, T3, T4, T5, T6,T7,TResult>> expression);
#endregion
}
public partial interface ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> : ISugarQueryable<T>
{
@ -385,6 +427,13 @@ namespace SqlSugar
ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> GroupBy(Expression<Func<T, T2, T3, T4, T5, T6, T7, object>> expression);
ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> GroupBy(Expression<Func<T, T2, T3, T4, T5, T6, T7, T8, object>> expression);
#endregion
#region Aggr
TResult Max<TResult>(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, TResult>> expression);
TResult Min<TResult>(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, TResult>> expression);
TResult Sum<TResult>(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, TResult>> expression);
TResult Avg<TResult>(Expression<Func<T, T2, T3, T4, T5, T6, T7,T8, TResult>> expression);
#endregion
}
}

Loading…
Cancel
Save