@@ -48,16 +48,17 @@ public Expression Compile(Expression target) {
4848 }
4949 }
5050
51- var groupKeyLambda = Expression . Lambda ( AnonType . CreateNewExpression ( groupKeyExprList , _anonTypeNewTweaks ) , groupByParam ) ;
51+ var groupKeyTypeFacade = new AnonTypeFacade ( groupKeyExprList ) ;
52+ var groupKeyLambda = Expression . Lambda ( groupKeyTypeFacade . CreateNewExpression ( _anonTypeNewTweaks ) , groupByParam ) ;
5253 var groupingType = typeof ( IGrouping < , > ) . MakeGenericType ( groupKeyLambda . ReturnType , ItemType ) ;
5354
5455 target = Expression . Call ( typeof ( Queryable ) , nameof ( Queryable . GroupBy ) , new [ ] { ItemType , groupKeyLambda . ReturnType } , target , Expression . Quote ( groupKeyLambda ) ) ;
5556
5657 for ( var i = 0 ; i < groupKeyExprList . Count ; i ++ ) {
5758 var orderParam = Expression . Parameter ( groupingType , "g" ) ;
58- var orderAccessor = Expression . Field (
59+ var orderAccessor = groupKeyTypeFacade . CreateMemberAccessor (
5960 Expression . Property ( orderParam , "Key" ) ,
60- AnonType . IndexToField ( i )
61+ i
6162 ) ;
6263
6364 target = Expression . Call (
@@ -69,25 +70,27 @@ public Expression Compile(Expression target) {
6970 ) ;
7071 }
7172
72- return MakeAggregatingProjection ( target , groupingType , groupKeyExprList . Count ) ;
73+ return MakeAggregatingProjection ( target , groupingType , groupKeyTypeFacade ) ;
7374 }
7475
75- Expression MakeAggregatingProjection ( Expression target , Type groupingType , int groupCount ) {
76+ Expression MakeAggregatingProjection ( Expression target , Type groupingType , AnonTypeFacade groupKeyTypeFacade ) {
7677 var param = Expression . Parameter ( groupingType , "g" ) ;
78+ var groupCount = groupKeyTypeFacade . MemberCount ;
7779
7880 var projectionExprList = new List < Expression > {
7981 Expression . Call ( typeof ( Enumerable ) , nameof ( Enumerable . Count ) , new [ ] { ItemType } , param )
8082 } ;
8183
8284 for ( var i = 0 ; i < groupCount ; i ++ )
83- projectionExprList . Add ( Expression . Field ( Expression . Property ( param , "Key" ) , AnonType . IndexToField ( i ) ) ) ;
85+ projectionExprList . Add ( groupKeyTypeFacade . CreateMemberAccessor ( Expression . Property ( param , "Key" ) , i ) ) ;
8486
8587 projectionExprList . AddRange ( MakeAggregates ( param , _totalSummary ) ) ;
8688
8789 if ( groupCount > 0 )
8890 projectionExprList . AddRange ( MakeAggregates ( param , _groupSummary ) ) ;
8991
90- var projectionLambda = Expression . Lambda ( AnonType . CreateNewExpression ( projectionExprList , _anonTypeNewTweaks ) , param ) ;
92+ var projectionTypeFacade = new AnonTypeFacade ( projectionExprList ) ;
93+ var projectionLambda = Expression . Lambda ( projectionTypeFacade . CreateNewExpression ( _anonTypeNewTweaks ) , param ) ;
9194
9295 return Expression . Call ( typeof ( Queryable ) , nameof ( Queryable . Select ) , new [ ] { param . Type , projectionLambda . ReturnType } , target , Expression . Quote ( projectionLambda ) ) ;
9396 }
0 commit comments