fixed that if the last is a select many it needs to check for null as well :)

This commit is contained in:
David Lebée 2018-04-11 22:04:54 -05:00
parent e961c38c99
commit 20da18bbdf
6 changed files with 35 additions and 10 deletions

View File

@ -97,9 +97,18 @@ namespace PoweredSoft.DynamicLinq.Test
t.PathToList("Posts.Comments.CommentLikes", selectCollectionHandling: SelectCollectionHandling.Flatten); t.PathToList("Posts.Comments.CommentLikes", selectCollectionHandling: SelectCollectionHandling.Flatten);
}); });
var abc = querySelect.ToObjectList(); var b = querySelect.ToDynamicClassList();
var list = querySelect.ToDynamicClassList(); Assert.AreEqual(a.Count, b.Count);
for(var i = 0; i < a.Count; i++)
{
var left = a[i];
var right = b[i];
var leftCommentLikes = left.CommentLikes;
var rightCommentLikes = right.GetDynamicPropertyValue<List<CommentLike>>("CommentLikes");
QueryableAssert.AreEqual(leftCommentLikes.AsQueryable(), rightCommentLikes.AsQueryable());
}
} }
} }
} }

View File

@ -133,7 +133,22 @@ namespace PoweredSoft.DynamicLinq.Test
Id = 1, Id = 1,
DisplayName = "John Doe", DisplayName = "John Doe",
CommentText = "!@#$!@#!@#", CommentText = "!@#$!@#!@#",
Email = "john.doe@me.com" Email = "john.doe@me.com",
CommentLikes = new List<CommentLike>()
{
new CommentLike()
{
Id = 1,
CommentId = 1,
CreateTime = DateTimeOffset.Now
},
new CommentLike()
{
Id = 2,
CommentId = 1,
CreateTime = DateTimeOffset.Now
},
}
} }
} }
}, },

View File

@ -62,11 +62,12 @@ namespace PoweredSoft.DynamicLinq.Resolver
var parentExpression = CompileGroup(parent, NullChecking); var parentExpression = CompileGroup(parent, NullChecking);
// check null with where. // check null with where.
var isSelectMany = CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(groupExpression);
if (NullChecking != false) if (NullChecking != false)
parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup); parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup && !isSelectMany);
// the select expression. // the select expression.
if (CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(groupExpression)) if (isSelectMany)
{ {
var selectType = parent.GroupEnumerableType(); var selectType = parent.GroupEnumerableType();
var selectExpression = Expression.Call(typeof(Enumerable), "SelectMany", var selectExpression = Expression.Call(typeof(Enumerable), "SelectMany",
@ -98,11 +99,11 @@ namespace PoweredSoft.DynamicLinq.Resolver
var parentExpression = CompileGroup(parent, NullChecking); var parentExpression = CompileGroup(parent, NullChecking);
var selectType = parent.GroupEnumerableType(); var selectType = parent.GroupEnumerableType();
bool isSelectMany = CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(currentExpression);
if (NullChecking != false) if (NullChecking != false)
parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup); parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup && !isSelectMany);
if (CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(currentExpression)) if (isSelectMany)
{ {
var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter); var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter);
currentExpression = Expression.Call(typeof(Enumerable), "SelectMany", currentExpression = Expression.Call(typeof(Enumerable), "SelectMany",
@ -147,10 +148,10 @@ namespace PoweredSoft.DynamicLinq.Resolver
return Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type); return Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type);
} }
private static Expression CheckNullOnEnumerableParent(ExpressionParserPieceGroup group, ExpressionParserPieceGroup parent, Expression parentExpression, bool isLastGroup) private static Expression CheckNullOnEnumerableParent(ExpressionParserPieceGroup group, ExpressionParserPieceGroup parent, Expression parentExpression, bool shouldSkipLast)
{ {
string path = null; string path = null;
if (isLastGroup) if (shouldSkipLast)
path = string.Join(".", group.Pieces.Take(group.Pieces.Count - 1).Select(t => t.Name)); path = string.Join(".", group.Pieces.Take(group.Pieces.Count - 1).Select(t => t.Name));
else else
path = string.Join(".", group.Pieces.Select(t => t.Name)); path = string.Join(".", group.Pieces.Select(t => t.Name));