From d98ca103595b5b6a3affb21ee33fc7b2ca430e22 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 24 Mar 2022 11:02:20 +0800 Subject: [PATCH] Update exp to sql .Week --- .../SqlServerTest/UnitTest/UCustom011.cs | 2 +- .../MethodCallExpressionResolve.cs | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom011.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom011.cs index beb62c4c5..3cd094312 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom011.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom011.cs @@ -18,7 +18,7 @@ namespace OrmTest var x2 = db.Queryable().Select(it => (DateTime.Now - SqlFunc.GetDate().AddDays(-1)).TotalHours).ToList(); var x3 = db.Queryable().Select(it => (DateTime.Now - SqlFunc.GetDate().AddHours(-1)).TotalMinutes).ToList(); var x4 = db.Queryable().Select(it => (DateTime.Now - SqlFunc.GetDate().AddMinutes(-1)).TotalSeconds).ToList(); - var x5 = db.Queryable().Select(it =>it.CreateTime.DayOfWeek).ToList(); + var x5 = db.Queryable().Select(it => new { x = DateTime.Now.DayOfWeek.ToString() }).ToList(); if (x1.Any()) { Check.Exception(x1.First()!=365, "unit error . UCustom011"); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index 1a0f69edb..bb29335be 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -559,6 +559,10 @@ namespace SqlSugar { parameter.CommonTempData = item.ToString(); } + else if (IsDateItemValue(item)) + { + parameter.CommonTempData = GetNewExpressionValue(item); + } else { base.Start(); @@ -581,7 +585,7 @@ namespace SqlSugar methodCallExpressionArgs.IsMember = false; } } - if (IsDateDate(item) || IsDateValue(item)) + if (IsDateDate(item) || IsDateValue(item)|| IsDateItemValue(item)) { methodCallExpressionArgs.IsMember = true; } @@ -604,6 +608,33 @@ namespace SqlSugar parameter.ChildExpression = null; } + private bool IsDateItemValue(Expression item) + { + var result = false; + if (item is MemberExpression) + { + var memberExp = item as MemberExpression; + if (memberExp != null && memberExp.Expression != null && memberExp.Expression.Type == UtilConstants.DateType) + { + foreach (var dateType in UtilMethods.EnumToDictionary()) + { + if (memberExp.Member.Name.EqualCase(dateType.Key)) + { + result = true; + break; + } + else if (memberExp.Member.Name=="DayOfWeek") + { + result = true; + break; + } + } + } + } + + return result; + } + private static bool IsDateDate(Expression item) { return item.Type == UtilConstants.DateType && item is MemberExpression && (item as MemberExpression).Member.Name == "Date" && item.ToString() != "DateTime.Now.Date";