From e961c38c998dad75142c5a5cc21a3bab5f6a9b54 Mon Sep 17 00:00:00 2001 From: David Lebee Date: Wed, 11 Apr 2018 21:39:54 -0500 Subject: [PATCH] select many support for flattening. --- .../Resolver/PathExpressionResolver.cs | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs b/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs index 7d8d9d7..f7c1959 100644 --- a/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs +++ b/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs @@ -66,11 +66,22 @@ namespace PoweredSoft.DynamicLinq.Resolver parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup); // the select expression. - var selectType = parent.GroupEnumerableType(); - var selectExpression = Expression.Call(typeof(Enumerable), "Select", - new Type[] { selectType, groupExpression.Type }, - parentExpression, groupExpressionLambda); - currentExpression = selectExpression; + if (CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(groupExpression)) + { + var selectType = parent.GroupEnumerableType(); + var selectExpression = Expression.Call(typeof(Enumerable), "SelectMany", + new Type[] { selectType, groupExpression.Type.GenericTypeArguments.First() }, + parentExpression, groupExpressionLambda); + currentExpression = selectExpression; + } + else + { + var selectType = parent.GroupEnumerableType(); + var selectExpression = Expression.Call(typeof(Enumerable), "Select", + new Type[] { selectType, groupExpression.Type }, + parentExpression, groupExpressionLambda); + currentExpression = selectExpression; + } } else { @@ -91,10 +102,20 @@ namespace PoweredSoft.DynamicLinq.Resolver if (NullChecking != false) parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup); - var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter); - currentExpression = Expression.Call(typeof(Enumerable), "Select", - new Type[] { selectType, currentExpression.Type }, - parentExpression, currentExpressionLambda); + if (CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(currentExpression)) + { + var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter); + currentExpression = Expression.Call(typeof(Enumerable), "SelectMany", + new Type[] { selectType, currentExpression.Type.GenericTypeArguments.First() }, + parentExpression, currentExpressionLambda); + } + else + { + var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter); + currentExpression = Expression.Call(typeof(Enumerable), "Select", + new Type[] { selectType, currentExpression.Type }, + parentExpression, currentExpressionLambda); + } } }