Skip to content

Commit

Permalink
Fix using indexes on sub Collections bug #36 (#37)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alirezanet authored Nov 16, 2021
1 parent b03a112 commit 7b3612c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
Binary file modified .gitattributes
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Gridify.EntityFramework/Gridify.EntityFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Gridify.EntityFramework</PackageId>
<Version>2.3.2</Version>
<Version>2.3.3</Version>
<Authors>Alireza Sabouri</Authors>
<Company>TuxTeam</Company>
<PackageDescription>Gridify (EntityFramework), Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data.</PackageDescription>
Expand Down
2 changes: 1 addition & 1 deletion src/Gridify/Gridify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Gridify</PackageId>
<Version>2.3.2</Version>
<Version>2.3.3</Version>
<Authors>Alireza Sabouri</Authors>
<Company>TuxTeam</Company>
<PackageDescription>Gridify, Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data.</PackageDescription>
Expand Down
9 changes: 3 additions & 6 deletions src/Gridify/Syntax/SyntaxTreeToQueryConvertor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ private static (Expression<Func<T, bool>> 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<Func<T, bool>>? GenerateNestedExpression<T>(
Expand Down Expand Up @@ -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);
Expand Down
86 changes: 86 additions & 0 deletions test/Gridify.Tests/Issue36Tests.cs
Original file line number Diff line number Diff line change
@@ -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<Level1> level1List = new List<Level1>();
level1List.Add(new Level1());

var gridifyMapper = new GridifyMapper<Level1>().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<Level2> Level2List = new List<Level2>()
{
new Level2()
{
Name = "1",
Level3 = new Level3()
{
Name = "2",
Level4List = new List<Level4>()
{
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<Level4>()
{
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<Level4> Level4List = new List<Level4>();
}

public class Level4
{
public string Name { get; set; }
public double Property1 { get; set; }
public double Property2 { get; set; }
}
}
}

0 comments on commit 7b3612c

Please sign in to comment.