better handling of null and simple expression null checking.

This commit is contained in:
David Lebée 2018-04-14 23:26:42 -05:00
parent 904d3c9945
commit 62fa1a4d21
4 changed files with 20 additions and 7 deletions

View File

@ -15,7 +15,7 @@ namespace PoweredSoft.DynamicLinq.ConsoleApp
static void Main(string[] args) static void Main(string[] args)
{ {
var selectTests = new SelectTests(); var selectTests = new SelectTests();
selectTests.SelectNullChecking(); selectTests.SelectNullChecking2();
} }
} }

View File

@ -82,6 +82,7 @@ namespace PoweredSoft.DynamicLinq.Test
{ {
var query = TestData.Authors.AsQueryable(); var query = TestData.Authors.AsQueryable();
var qs = query.Select(t => new var qs = query.Select(t => new
{ {
CommentLikes = t.Posts == null ? CommentLikes = t.Posts == null ?
@ -128,7 +129,8 @@ namespace PoweredSoft.DynamicLinq.Test
{ {
t.NullChecking(true); t.NullChecking(true);
// this needs to be fixed. // 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(); var b = querySelect.ToDynamicClassList();

View File

@ -208,7 +208,11 @@ namespace PoweredSoft.DynamicLinq.Helpers
{ {
if (selectType == SelectTypes.Path) 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) else if (selectType == SelectTypes.PathToList)
{ {

View File

@ -15,7 +15,6 @@ namespace PoweredSoft.DynamicLinq.Resolver
public ExpressionParser Parser { get; protected set; } public ExpressionParser Parser { get; protected set; }
public Expression Result { get; protected set; } public Expression Result { get; protected set; }
public Type NullType { get; set; }
public PathExpressionResolver(ExpressionParser parser) public PathExpressionResolver(ExpressionParser parser)
{ {
@ -55,7 +54,11 @@ namespace PoweredSoft.DynamicLinq.Resolver
if (group.Parent == null) if (group.Parent == null)
{ {
currentExpression = groupExpressionLambda; currentExpression = groupExpression;
if (NullChecking != false)
currentExpression = CheckNullOnFirstGroup(group, currentExpression);
currentExpression = Expression.Lambda(currentExpression, group.Parameter);
continue; continue;
} }
@ -128,10 +131,14 @@ namespace PoweredSoft.DynamicLinq.Resolver
{ {
var path = string.Join(".", group.Pieces.Select(t => t.Name)); var path = string.Join(".", group.Pieces.Select(t => t.Name));
var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path, var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path,
ConditionOperators.Equal, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
parameter: group.Parameter, nullChecking: true); parameter: group.Parameter, nullChecking: true);
var whereBodyExpression = (whereExpression as LambdaExpression).Body; var whereBodyExpression = (whereExpression as LambdaExpression).Body;
whereBodyExpression =Expression.Not(whereBodyExpression);
var nullType = currentExpression.Type; var nullType = currentExpression.Type;
Expression ifTrueExpression = null; Expression ifTrueExpression = null;
@ -143,7 +150,7 @@ namespace PoweredSoft.DynamicLinq.Resolver
} }
else else
{ {
ifTrueExpression = Expression.Default(NullType); ifTrueExpression = Expression.Default(nullType);
} }
return Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type); return Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type);