From 7b3612cc13df4cdc9267aee6668c66fe82f4b300 Mon Sep 17 00:00:00 2001 From: AliReZa Sabouri <7004080+alirezanet@users.noreply.github.com> Date: Tue, 16 Nov 2021 12:19:52 +0330 Subject: [PATCH] Fix using indexes on sub Collections bug #36 (#37) * Introduce end-of-line normalization * bug fix using fields instead of properties * fix bug #36 * make sure returns single record * update to v2.3.3 --- .gitattributes | Bin 5160 -> 16 bytes .../Gridify.EntityFramework.csproj | 2 +- src/Gridify/Gridify.csproj | 2 +- .../Syntax/SyntaxTreeToQueryConvertor.cs | 9 +- test/Gridify.Tests/Issue36Tests.cs | 86 ++++++++++++++++++ 5 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 test/Gridify.Tests/Issue36Tests.cs diff --git a/.gitattributes b/.gitattributes index 1e8dfcf2b284342ec2e7daa0cfc662588d3d038b..f81619613d18447d5baaf4c9bf47692b18dce762 100644 GIT binary patch literal 16 Xcma#nisj`hNv$ZcO)M?R=j8$bD6Ry| literal 5160 zcmdUz%We}<42I7diFY_sH&7uS0I3VqiX9h0gxKaXDJi)zleC402ma32%`?vkuK9j$HPSR68o~LKRm(!P2q?LY0`W>f!TBoVq4|M-s_t$BX7OB*Vm436W z;A*C;mEH~YJJs<^@8-6WH&ea&Ee+YO=5|%+{YcN}=`vO6b#eqxJw~VId691A?b*hA zupbBh6XD-K$KyKJL(4FkBxgA-#Q<`?G;WZ^v5v@LaH%K8OqAPPpUHb+@}ako#?m-Z z7^eo}+eG&xlUhh;mX%6us>G6sSz8$ooudosLqAO8i8B61+n2jR6(fh7v*m2IUCF4F~HLD(oQxiEVfjArsTTT@LVv{#VWc$pX zN6hf&SbS76ZJV~Wbnfof2F5GV_H-~rTk9CT6&+et>KQx!_PUr zuWfk8gJWJpbW%srbdz40HmVlQfi5KP z_c-uVTai2TW2>xjycCyny092{XK6=`D0gbcINN%YxF3DV@0uzZy#IJM@hly!=o`1 zv)Q3cw{Sij!%B6xEkULqmb$U3ZpUMn)Ui~H_r>s{7g4y4s_Sh z=X^pooz`dDUH#Pee(k+x>?Dt)-&~1-mvy(fySFUWZ+5R>$IhJP=;}BgHZ^;W|G$BQ zS225h2gsBfY9>)l%sJmZl*TJ|RKc|PJ=gkqJ^5I@9V?%CyRuaGU3@=D#}EBUi;2ezepd`xgyB1zm(g$Jy6PReLGMJ mTQK=BgxKmGEVWb&qRZ?bjZcc=yMt} netstandard2.0 Gridify.EntityFramework - 2.3.2 + 2.3.3 Alireza Sabouri TuxTeam Gridify (EntityFramework), Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data. diff --git a/src/Gridify/Gridify.csproj b/src/Gridify/Gridify.csproj index b62cf6d6..abcc80f2 100644 --- a/src/Gridify/Gridify.csproj +++ b/src/Gridify/Gridify.csproj @@ -3,7 +3,7 @@ netstandard2.0 Gridify - 2.3.2 + 2.3.3 Alireza Sabouri TuxTeam Gridify, Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data. diff --git a/src/Gridify/Syntax/SyntaxTreeToQueryConvertor.cs b/src/Gridify/Syntax/SyntaxTreeToQueryConvertor.cs index fa39ddb3..5f91adc8 100644 --- a/src/Gridify/Syntax/SyntaxTreeToQueryConvertor.cs +++ b/src/Gridify/Syntax/SyntaxTreeToQueryConvertor.cs @@ -43,11 +43,8 @@ private static (Expression> Expression, bool IsNested)? ConvertBin private static LambdaExpression UpdateExpressionIndex(LambdaExpression exp, int index) { - var parameter = exp.Parameters[0]; - var unary = exp.Body as UnaryExpression; - var body = unary!.Operand as MemberExpression; - var newBody = new PredicateBuilder.ReplaceExpressionVisitor(exp.Parameters[1], Expression.Constant(index, typeof(int))).Visit(body!); - return Expression.Lambda(newBody, parameter); + var body = new PredicateBuilder.ReplaceExpressionVisitor(exp.Parameters[1], Expression.Constant(index, typeof(int))).Visit(exp.Body); + return Expression.Lambda(body, exp.Parameters); } private static Expression>? GenerateNestedExpression( @@ -107,7 +104,7 @@ private static ParameterExpression GetParameterExpression(MemberExpression membe private static LambdaExpression GetAnyExpression(MemberExpression member, Expression predicate) { var param = GetParameterExpression(member); - var prop = Expression.Property(param!, member.Member.Name); + var prop = Expression.PropertyOrField(param!, member.Member.Name); var tp = prop.Type.GenericTypeArguments[0]; var anyMethod = GetAnyMethod(tp); diff --git a/test/Gridify.Tests/Issue36Tests.cs b/test/Gridify.Tests/Issue36Tests.cs new file mode 100644 index 00000000..67b26b94 --- /dev/null +++ b/test/Gridify.Tests/Issue36Tests.cs @@ -0,0 +1,86 @@ +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace Gridify.Tests +{ + // issue #36 - https://github.com/alirezanet/Gridify/issues/36 + public class Issue36Tests + { + [Fact] + private void UserReportTest1() + { + List level1List = new List(); + level1List.Add(new Level1()); + + var gridifyMapper = new GridifyMapper().GenerateMappings() + .AddMap("level4_property1", (l1, index) => l1.Level2List.Select(x => x.Level3.Level4List[index].Property1)); + + var gridifyQuery = new GridifyQuery() { Filter = "level4_property1[0] > 5" }; + var query = gridifyQuery.GetFilteringExpression(gridifyMapper); + var expression = query.Compile(); + + var actual = level1List.Where(expression).ToList(); + + Assert.Single(actual); + Assert.True(actual.Any()); + } + + public class Level1 + { + public string Name { get; set; } + + public List Level2List = new List() + { + new Level2() + { + Name = "1", + Level3 = new Level3() + { + Name = "2", + Level4List = new List() + { + new Level4() { Name = "3", Property1 = 3, Property2 = 4 }, + new Level4() { Name = "4", Property1 = 4, Property2 = 5 }, + new Level4() { Name = "5", Property1 = 5, Property2 = 6 } + } + } + }, + + new Level2() + { + Name = "6", + Level3 = new Level3() + { + Name = "7", + Level4List = new List() + { + new Level4() { Name = "8", Property1 = 8, Property2 = 9 }, + new Level4() { Name = "9", Property1 = 9, Property2 = 10 }, + new Level4() { Name = "10", Property1 = 10, Property2 = 11 } + } + } + }, + }; + } + + public class Level2 + { + public string Name { get; set; } + public Level3 Level3 = new Level3(); + } + + public class Level3 + { + public string Name { get; set; } + public List Level4List = new List(); + } + + public class Level4 + { + public string Name { get; set; } + public double Property1 { get; set; } + public double Property2 { get; set; } + } + } +} \ No newline at end of file