Browse Source

Update Orderby

pull/15/head
sunkaixuan 3 years ago
parent
commit
31acb68743
  1. 5
      Src/Asp.Net/SqlServerTest/Demo/Demo1_Queryable.cs
  2. 23
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  3. 9
      Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/DefaultDbMethod.cs
  4. 4
      Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/IDbMethods.cs
  5. 9
      Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs
  6. 4
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs

5
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<Order>().Queryable<Order>().ToList();
var test34 = db.Queryable<Order>().OrderBy(it=>new {
id= it.Id,
name=SqlFunc.Asc(it.Name),
}).ToList();
Console.WriteLine("#### Examples End ####");
}

23
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();

9
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 ";
}
}
}

4
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);
}
}

9
Src/Asp.Net/SqlSugar/ExpressionsToSql/DbMethods/SqlFunc.cs

@ -9,7 +9,14 @@ namespace SqlSugar
{
public partial class SqlFunc
{
public static T Desc<T>(T value)
{
throw new NotSupportedException("Can only be used in expressions");
}
public static T Asc<T>(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");

4
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;
}

Loading…
Cancel
Save