From 18a0fe26b637ba8d806190f8ead56538612d97b9 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 23 Mar 2022 18:02:25 +0800 Subject: [PATCH] Support date.DayOfWeek --- .../SqlServerTest/UnitTest/UCustom011.cs | 1 + .../ExpressionsToSql/Common/DateType.cs | 3 ++- .../ResolveItems/MemberExpressionResolve.cs | 26 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom011.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom011.cs index 4766376f9..beb62c4c5 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UCustom011.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UCustom011.cs @@ -18,6 +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(); if (x1.Any()) { Check.Exception(x1.First()!=365, "unit error . UCustom011"); diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/DateType.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/DateType.cs index 6de1122b4..bb5f71d76 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/DateType.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/Common/DateType.cs @@ -14,6 +14,7 @@ namespace SqlSugar Hour = 4, Second=5, Minute=6, - Millisecond=7 + Millisecond=7, + Weekday=8 } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs index a950234ea..ad95598fc 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs @@ -23,6 +23,10 @@ namespace SqlSugar else if (IsDateDiff(expression)) { ResolveDateDiff(parameter, isLeft, expression); + } + else if (expression.Member.Name== "DayOfWeek"&& expression.Type==typeof(DayOfWeek)) + { + ResolveDayOfWeek(parameter, isLeft, expression); } else if (isHasValue) { @@ -212,6 +216,28 @@ namespace SqlSugar #endregion #region Resolve special member + private void ResolveDayOfWeek(ExpressionParameter parameter, bool? isLeft, MemberExpression expression) + { + var exp = expression.Expression; + var value = GetNewExpressionValue(exp); + var result = this.Context.DbMehtods.DateValue(new MethodCallExpressionModel() + { + Args = new List() { + + new MethodCallExpressionArgs(){ + MemberName=value, + MemberValue=value + }, + new MethodCallExpressionArgs(){ + MemberName=DateType.Weekday, + MemberValue=DateType.Weekday + } + } + }); ; + base.AppendMember(parameter, isLeft, result); + } + + private void ResolveDateDiff(ExpressionParameter parameter, bool? isLeft, MemberExpression expression) { var binaryExp=expression.Expression as BinaryExpression;