From 31acb687433e23b2069e33be4c772ce5b1866535 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 9 Apr 2022 17:02:31 +0800 Subject: [PATCH] Update Orderby --- .../SqlServerTest/Demo/Demo1_Queryable.cs | 5 ++++ .../QueryableProvider/QueryableProvider.cs | 23 ++++++++++++++++++- .../DbMethods/DefaultDbMethod.cs | 9 ++++++++ .../ExpressionsToSql/DbMethods/IDbMethods.cs | 4 +++- .../ExpressionsToSql/DbMethods/SqlFunc.cs | 9 +++++++- .../MethodCallExpressionResolve.cs | 4 ++++ 6 files changed, 51 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo1_Queryable.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo1_Queryable.cs index 1129da6e6..a611fec2c 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/Demo1_Queryable.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo1_Queryable.cs @@ -139,6 +139,11 @@ namespace OrmTest }; var five=db.Ado.ExecuteCommand("11111"); db.CurrentConnectionConfig.SqlMiddle = null; + db.GetConnectionWithAttr().Queryable().ToList(); + var test34 = db.Queryable().OrderBy(it=>new { + id= it.Id, + name=SqlFunc.Asc(it.Name), + }).ToList(); Console.WriteLine("#### Examples End ####"); } diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index a87a01915..d7794a4a4 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1911,7 +1911,28 @@ namespace SqlSugar { QueryBuilder.CheckExpression(expression, "OrderBy"); var isSingle = QueryBuilder.IsSingle(); - if ((expression as LambdaExpression).Body is NewExpression) + if (expression.ToString().IsContainsIn("Desc(", "Asc(")) + { + var orderValue = ""; + var newExp = (expression as LambdaExpression).Body as NewExpression; + foreach (var item in newExp.Arguments) + { + if (item is MemberExpression) + { + orderValue += + QueryBuilder.GetExpressionValue(item, isSingle ? ResolveExpressType.FieldSingle : ResolveExpressType.FieldMultiple).GetResultString() + ","; + } + else + { + orderValue += + QueryBuilder.GetExpressionValue(item, isSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple).GetResultString() + ","; + } + } + orderValue = orderValue.TrimEnd(','); + OrderBy(orderValue); + return this; + } + else if ((expression as LambdaExpression).Body is NewExpression) { var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.ArraySingle : ResolveExpressType.ArrayMultiple); var items = lamResult.GetResultString().Split(',').Where(it => it.HasValue()).Select(it => it + UtilConstants.Space + type.ToString().ToUpper()).ToList(); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs index ff3180088..908c832a6 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs @@ -526,5 +526,14 @@ namespace SqlSugar var parameter2 = model.Args[1]; return string.Format(" ({0} <= {1}) ", parameter.MemberName, parameter2.MemberName); } + + public virtual string Asc(MethodCallExpressionModel model) + { + return model.Args[0].MemberName + " ASC "; + } + public virtual string Desc(MethodCallExpressionModel model) + { + return model.Args[0].MemberName + " DESC "; + } } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs index b64a57828..9c96a9b1d 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs @@ -80,6 +80,8 @@ namespace SqlSugar string GreaterThan(MethodCallExpressionModel model); string GreaterThanOrEqual(MethodCallExpressionModel model); string LessThan(MethodCallExpressionModel model); - string LessThanOrEqual(MethodCallExpressionModel model); + string LessThanOrEqual(MethodCallExpressionModel model); + string Asc(MethodCallExpressionModel model); + string Desc(MethodCallExpressionModel model); } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs index 90214ed85..23f80679f 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs @@ -9,7 +9,14 @@ namespace SqlSugar { public partial class SqlFunc { - + public static T Desc(T value) + { + throw new NotSupportedException("Can only be used in expressions"); + } + public static T Asc(T value) + { + throw new NotSupportedException("Can only be used in expressions"); + } public static int DateDiff(DateType dateType,DateTime littleTime, DateTime bigTime) { throw new NotSupportedException("Can only be used in expressions"); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index 1b623c10a..52763c958 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -910,6 +910,10 @@ namespace SqlSugar return this.Context.DbMehtods.LessThan(model); case "LessThanOrEqual": return this.Context.DbMehtods.LessThanOrEqual(model); + case "Asc": + return this.Context.DbMehtods.Asc(model); + case "Desc": + return this.Context.DbMehtods.Desc(model); default: break; }