From 62fa1a4d21d69064e387df53315a32c07a775c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Leb=C3=A9e?= Date: Sat, 14 Apr 2018 23:26:42 -0500 Subject: [PATCH] better handling of null and simple expression null checking. --- PoweredSoft.DynamicLinq.ConsoleApp/Program.cs | 2 +- PoweredSoft.DynamicLinq.Test/SelectTests.cs | 4 +++- .../Helpers/QueryableHelpers.cs | 6 +++++- .../Resolver/PathExpressionResolver.cs | 15 +++++++++++---- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/PoweredSoft.DynamicLinq.ConsoleApp/Program.cs b/PoweredSoft.DynamicLinq.ConsoleApp/Program.cs index a933f2d..2fc0be0 100644 --- a/PoweredSoft.DynamicLinq.ConsoleApp/Program.cs +++ b/PoweredSoft.DynamicLinq.ConsoleApp/Program.cs @@ -15,7 +15,7 @@ namespace PoweredSoft.DynamicLinq.ConsoleApp static void Main(string[] args) { var selectTests = new SelectTests(); - selectTests.SelectNullChecking(); + selectTests.SelectNullChecking2(); } } diff --git a/PoweredSoft.DynamicLinq.Test/SelectTests.cs b/PoweredSoft.DynamicLinq.Test/SelectTests.cs index 531c2b5..36bd62f 100644 --- a/PoweredSoft.DynamicLinq.Test/SelectTests.cs +++ b/PoweredSoft.DynamicLinq.Test/SelectTests.cs @@ -82,6 +82,7 @@ namespace PoweredSoft.DynamicLinq.Test { var query = TestData.Authors.AsQueryable(); + var qs = query.Select(t => new { CommentLikes = t.Posts == null ? @@ -128,7 +129,8 @@ namespace PoweredSoft.DynamicLinq.Test { t.NullChecking(true); // this needs to be fixed. - t.PathToList("Comment.Post.Comments.CommentText", selectCollectionHandling: SelectCollectionHandling.Flatten); + t.Path("Comment.CommentText", "CommentText2"); + //t.PathToList("Comment.Post.Comments.CommentText", selectCollectionHandling: SelectCollectionHandling.Flatten); }); var b = querySelect.ToDynamicClassList(); diff --git a/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs b/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs index 8fd42f0..a53bf21 100644 --- a/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs +++ b/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs @@ -208,7 +208,11 @@ namespace PoweredSoft.DynamicLinq.Helpers { if (selectType == SelectTypes.Path) { - return ResolvePathForExpression(parameter, path); + var parser = new ExpressionParser(parameter, path); + var resolver = new PathExpressionResolver(parser); + resolver.NullChecking = nullChecking; + resolver.Resolve(); + return resolver.GetResultBodyExpression(); } else if (selectType == SelectTypes.PathToList) { diff --git a/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs b/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs index a59b425..9871beb 100644 --- a/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs +++ b/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs @@ -15,7 +15,6 @@ namespace PoweredSoft.DynamicLinq.Resolver public ExpressionParser Parser { get; protected set; } public Expression Result { get; protected set; } - public Type NullType { get; set; } public PathExpressionResolver(ExpressionParser parser) { @@ -55,7 +54,11 @@ namespace PoweredSoft.DynamicLinq.Resolver if (group.Parent == null) { - currentExpression = groupExpressionLambda; + currentExpression = groupExpression; + if (NullChecking != false) + currentExpression = CheckNullOnFirstGroup(group, currentExpression); + + currentExpression = Expression.Lambda(currentExpression, group.Parameter); continue; } @@ -128,10 +131,14 @@ namespace PoweredSoft.DynamicLinq.Resolver { var path = string.Join(".", group.Pieces.Select(t => t.Name)); var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path, - ConditionOperators.Equal, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, + ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, parameter: group.Parameter, nullChecking: true); var whereBodyExpression = (whereExpression as LambdaExpression).Body; + whereBodyExpression =Expression.Not(whereBodyExpression); + + + var nullType = currentExpression.Type; Expression ifTrueExpression = null; @@ -143,7 +150,7 @@ namespace PoweredSoft.DynamicLinq.Resolver } else { - ifTrueExpression = Expression.Default(NullType); + ifTrueExpression = Expression.Default(nullType); } return Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type);