From 3f447443258ff306409ad48b34aee6be86439d0c Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Mon, 18 Jul 2022 23:30:05 +0800 Subject: [PATCH] =?UTF-8?q?[#172]=20bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Visitors/QueryableRouteDiscoverVisitorV2.cs | 12 ++++++++---- .../Visitors/ShardingExpressionVisitor.cs | 15 +++++++++++++++ test/ShardingCore.CommonTest/ShardingTableTime.cs | 4 +++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitorV2.cs b/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitorV2.cs index 103883a4..59dd3ee4 100644 --- a/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitorV2.cs +++ b/src/ShardingCore/Sharding/Visitors/QueryableRouteDiscoverVisitorV2.cs @@ -162,7 +162,8 @@ namespace ShardingCore.Core.Internal.Visitors member.Expression is MemberExpression)) || expression is MethodCallExpression || (expression is UnaryExpression unaryExpression && - unaryExpression.NodeType is ExpressionType.Convert); + unaryExpression.NodeType is ExpressionType.Convert) + || expression.NodeType == ExpressionType.ArrayIndex; } private bool IsMethodCall(Expression expression) @@ -460,13 +461,15 @@ namespace ShardingCore.Core.Internal.Visitors return RoutePredicateExpression.Default; } - private RoutePredicateExpression ParseNamedComparison(BinaryExpression binaryExpression,MethodCallExpression methodCallExpression) + private RoutePredicateExpression ParseNamedComparison(BinaryExpression binaryExpression, + MethodCallExpression methodCallExpression) { if (methodCallExpression.GetComparisonLeftAndRight(out var result)) { return ParseCompare(methodCallExpression, result.Left, result.Right, binaryExpression.NodeType, (int)GetExpressionValue(binaryExpression.Right)); } + return RoutePredicateExpression.Default; } @@ -557,7 +560,8 @@ namespace ShardingCore.Core.Internal.Visitors { return ParseConditionOnRight0(false, predicateRightResult, binaryExpression.Left, binaryExpression.NodeType); - } else if (binaryExpression.IsNamedComparison(out var methodCallExpression)) + } + else if (binaryExpression.IsNamedComparison(out var methodCallExpression)) { return ParseNamedComparison(binaryExpression, methodCallExpression); } @@ -589,7 +593,7 @@ namespace ShardingCore.Core.Internal.Visitors } if (binaryExpression.Left is UnaryExpression unaryExpression1 && - (binaryExpression.Right is MemberExpression )) + (binaryExpression.Right is MemberExpression)) left = Resolve(unaryExpression1); if (binaryExpression.Right is UnaryExpression unaryExpression2 && (binaryExpression.Left is MemberExpression)) diff --git a/src/ShardingCore/Sharding/Visitors/ShardingExpressionVisitor.cs b/src/ShardingCore/Sharding/Visitors/ShardingExpressionVisitor.cs index 35653026..0dc3a821 100644 --- a/src/ShardingCore/Sharding/Visitors/ShardingExpressionVisitor.cs +++ b/src/ShardingCore/Sharding/Visitors/ShardingExpressionVisitor.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -114,8 +115,22 @@ namespace ShardingCore.Core.Internal.Visitors } default: + { + if (expression is BinaryExpression binaryExpression&&expression.NodeType == ExpressionType.ArrayIndex) + { + var index = GetExpressionValue(binaryExpression.Right); + if (index is int i) + { + var arrayObject = GetExpressionValue(binaryExpression.Left); + if (arrayObject is IList list) + { + return list[i]; + } + } + } //TODO: better messaging throw new ShardingCoreException("cant get value " + expression); + } } } } diff --git a/test/ShardingCore.CommonTest/ShardingTableTime.cs b/test/ShardingCore.CommonTest/ShardingTableTime.cs index 11c06c5d..f723a6bb 100644 --- a/test/ShardingCore.CommonTest/ShardingTableTime.cs +++ b/test/ShardingCore.CommonTest/ShardingTableTime.cs @@ -91,11 +91,13 @@ namespace ShardingCore.CommonTest var id = "1"; var times = new []{queryTime}; var times1 = new List(){queryTime}; + var times2 = new []{queryTime,queryTime2}; var obj1 = new {time=new DateTime(2022, 1, 2)}; var queryables=new List>() { - // new List().AsQueryable().Where(o=>o.Time==times[0]), + new List().AsQueryable().Where(o=>o.Time==times2[0]), + new List().AsQueryable().Where(o=>o.Time>=times2[0]&&o.Time().AsQueryable().Where(o=>o.Time>=queryTime&&o.Time().AsQueryable().Where(o=>o.Time==queryTime), new List().AsQueryable().Where(o=>o.Time>=queryTime&&o.Time