Browse Source

Update exp to sql

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
5a3708172e
  1. 4
      Src/Asp.Net/SqlServerTest/Demos/1_Query.cs
  2. 30
      Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs

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

@ -447,6 +447,10 @@ namespace OrmTest.Demo
}).ToList();
var test46 = db.Queryable<Student>().Where(it => it.CreateTime > SqlFunc.ToDate(DateTime.Now.Date)).ToList();
var test47 = db.Queryable<Student>().Where(it =>string.IsNullOrEmpty(it.Name)==true).ToList();
var test48 = db.Queryable<Student>().Where(it=>it.CreateTime!=null).Where(it => SqlFunc.ToDate(it.CreateTime).Date==DateTime.Now.Date).ToList();
var test49 = db.Queryable<Student>().Where(it => it.CreateTime != null).Where(it => SqlFunc.ToDate(it.CreateTime).Year == DateTime.Now.Year).ToList();
var test50 = db.Queryable<Student>().Where(it => it.CreateTime != null).Where(it => SqlFunc.ToDate(it.CreateTime).Year == SqlFunc.GetDate().Year).ToList();
var test51 = db.Queryable<Student>().Select(it=>new { x= SqlFunc.ToDate(it.CreateTime).Year+"-" }).ToList();
}
public static void Page()
{

30
Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs

@ -36,6 +36,10 @@ namespace SqlSugar
{
ResolveValue(parameter, baseParameter, expression, isLeft, isSetTempData, isSingle);
}
else if (expression.Expression != null &&expression.Expression.Type==UtilConstants.DateType&&expression is MemberExpression && expression.Expression is MethodCallExpression)
{
ResolveDateDateByCall(parameter, isLeft, expression);
}
else if (isDateDate)
{
ResolveDateDate(parameter, isLeft, expression);
@ -54,6 +58,7 @@ namespace SqlSugar
}
}
#region Resolve default
private void ResolveDefault(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool? isLeft, bool isSetTempData, bool isSingle)
{
@ -164,6 +169,31 @@ namespace SqlSugar
#endregion
#region Resolve special member
private void ResolveDateDateByCall(ExpressionParameter parameter, bool? isLeft, MemberExpression expression)
{
var value = GetNewExpressionValue(expression.Expression);
if (expression.Member.Name == "Date")
{
AppendMember(parameter, isLeft, GetToDate(this.Context.DbMehtods.MergeString(
this.GetDateValue(value, DateType.Year),
"'-'",
this.GetDateValue(value, DateType.Month),
"'-'",
this.GetDateValue(value, DateType.Day))));
}
else
{
foreach (int myCode in Enum.GetValues(typeof(DateType)))
{
string strName = Enum.GetName(typeof(DateType), myCode);//获取名称
if (expression.Member.Name == strName)
{
AppendMember(parameter, isLeft, this.Context.DbMehtods.MergeString(this.GetDateValue(value, (DateType)(myCode))));
}
}
}
}
private MemberExpression ResolveValue(ExpressionParameter parameter, ExpressionParameter baseParameter, MemberExpression expression, bool? isLeft, bool isSetTempData, bool isSingle)
{
expression = expression.Expression as MemberExpression;

Loading…
Cancel
Save