diff --git a/PoweredSoft.DynamicLinq.Test/SelectTests.cs b/PoweredSoft.DynamicLinq.Test/SelectTests.cs index 34b9155..ff5ef9d 100644 --- a/PoweredSoft.DynamicLinq.Test/SelectTests.cs +++ b/PoweredSoft.DynamicLinq.Test/SelectTests.cs @@ -97,9 +97,18 @@ namespace PoweredSoft.DynamicLinq.Test 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>("CommentLikes"); + QueryableAssert.AreEqual(leftCommentLikes.AsQueryable(), rightCommentLikes.AsQueryable()); + } } } } diff --git a/PoweredSoft.DynamicLinq.Test/TestData.cs b/PoweredSoft.DynamicLinq.Test/TestData.cs index bedff34..2ab0a59 100644 --- a/PoweredSoft.DynamicLinq.Test/TestData.cs +++ b/PoweredSoft.DynamicLinq.Test/TestData.cs @@ -133,7 +133,22 @@ namespace PoweredSoft.DynamicLinq.Test Id = 1, DisplayName = "John Doe", CommentText = "!@#$!@#!@#", - Email = "john.doe@me.com" + Email = "john.doe@me.com", + CommentLikes = new List() + { + new CommentLike() + { + Id = 1, + CommentId = 1, + CreateTime = DateTimeOffset.Now + }, + new CommentLike() + { + Id = 2, + CommentId = 1, + CreateTime = DateTimeOffset.Now + }, + } } } }, diff --git a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.cs b/PoweredSoft.DynamicLinq/Fluent/Where/WhereBuilder.cs similarity index 100% rename from PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.cs rename to PoweredSoft.DynamicLinq/Fluent/Where/WhereBuilder.cs diff --git a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.shortcuts.cs b/PoweredSoft.DynamicLinq/Fluent/Where/WhereBuilder.shortcuts.cs similarity index 100% rename from PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.shortcuts.cs rename to PoweredSoft.DynamicLinq/Fluent/Where/WhereBuilder.shortcuts.cs diff --git a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderCondition.cs b/PoweredSoft.DynamicLinq/Fluent/Where/WhereBuilderCondition.cs similarity index 100% rename from PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderCondition.cs rename to PoweredSoft.DynamicLinq/Fluent/Where/WhereBuilderCondition.cs diff --git a/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs b/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs index f7c1959..f86cc0f 100644 --- a/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs +++ b/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs @@ -62,11 +62,12 @@ namespace PoweredSoft.DynamicLinq.Resolver var parentExpression = CompileGroup(parent, NullChecking); // check null with where. + var isSelectMany = CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(groupExpression); if (NullChecking != false) - parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup); + parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup && !isSelectMany); // the select expression. - if (CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(groupExpression)) + if (isSelectMany) { var selectType = parent.GroupEnumerableType(); var selectExpression = Expression.Call(typeof(Enumerable), "SelectMany", @@ -98,11 +99,11 @@ namespace PoweredSoft.DynamicLinq.Resolver var parentExpression = CompileGroup(parent, NullChecking); var selectType = parent.GroupEnumerableType(); - + bool isSelectMany = CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(currentExpression); 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); currentExpression = Expression.Call(typeof(Enumerable), "SelectMany", @@ -147,10 +148,10 @@ namespace PoweredSoft.DynamicLinq.Resolver 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; - if (isLastGroup) + if (shouldSkipLast) path = string.Join(".", group.Pieces.Take(group.Pieces.Count - 1).Select(t => t.Name)); else path = string.Join(".", group.Pieces.Select(t => t.Name));