private static InvocationExpressionSyntax Merge( InvocationExpressionSyntax outerMostInvocation, MemberAccessExpressionSyntax innerMostWhereAccess, List <ExpressionSyntax> selectArguments, SemanticModel semanticModel) { var firstArgument = selectArguments[0]; string parameterName; ParameterSyntax firstParameter; IdentifierNameSyntax firstParameterIdentifier; InvocationExpressionSyntax resultInvocation; if (firstArgument.IsKind(SyntaxKind.SimpleLambdaExpression)) { var lambda = (SimpleLambdaExpressionSyntax)firstArgument; firstParameter = lambda.Parameter; parameterName = firstParameter.Identifier.Text; firstParameterIdentifier = SyntaxFactory.IdentifierName(firstParameter.Identifier); resultInvocation = (InvocationExpressionSyntax)lambda.Body; } else { parameterName = NameHelper.GetLambdaParameterName( outerMostInvocation.SpanStart, semanticModel); var parameterIdentifier = SyntaxFactory .Identifier(parameterName) .WithAdditionalAnnotations(RenameAnnotation.Create()); firstParameter = SyntaxFactory.Parameter(parameterIdentifier); firstParameterIdentifier = SyntaxFactory.IdentifierName(parameterIdentifier); resultInvocation = ExtendedSyntaxFactory.MakeInvocation( firstArgument, firstParameterIdentifier); } for (int i = 1; i < selectArguments.Count; ++i) { if (selectArguments[i].IsKind(SyntaxKind.SimpleLambdaExpression)) { var currentLambda = (SimpleLambdaExpressionSyntax)selectArguments[i]; var currentParameter = currentLambda.Parameter; var currentParameterName = currentParameter.Identifier.Text; var parameterSymbol = semanticModel.GetDeclaredSymbol(currentParameter); var substituteRewriter = new SubstituteRewriter( currentParameterName, parameterSymbol, semanticModel, resultInvocation); resultInvocation = (InvocationExpressionSyntax)currentLambda .Body .Accept(substituteRewriter); } else { resultInvocation = ExtendedSyntaxFactory.MakeInvocation( selectArguments[i], resultInvocation); } } var newLambda = SyntaxFactory.SimpleLambdaExpression( firstParameter, resultInvocation); var newInvocation = ExtendedSyntaxFactory.MakeInvocation( innerMostWhereAccess, newLambda); return(newInvocation); }
private static InvocationExpressionSyntax Merge( InvocationExpressionSyntax outerMostInvocation, MemberAccessExpressionSyntax innerMostWhereAccess, List <ExpressionSyntax> whereArguments, SemanticModel semanticModel) { var firstArgument = whereArguments[0]; string parameterName; ParameterSyntax firstParameter; IdentifierNameSyntax firstParameterIdentifier; ExpressionSyntax filterExpression; if (firstArgument.IsKind(SyntaxKind.SimpleLambdaExpression)) { var lambda = (SimpleLambdaExpressionSyntax)firstArgument; firstParameter = lambda.Parameter; parameterName = firstParameter.Identifier.Text; firstParameterIdentifier = SyntaxFactory.IdentifierName(firstParameter.Identifier); filterExpression = MakeExpressionFromLambdaBody(lambda.Body); } else { parameterName = NameHelper.GetLambdaParameterName( outerMostInvocation.SpanStart, semanticModel); var parameterIdentifier = SyntaxFactory .Identifier(parameterName) .WithAdditionalAnnotations(RenameAnnotation.Create()); firstParameter = SyntaxFactory.Parameter(parameterIdentifier); firstParameterIdentifier = SyntaxFactory.IdentifierName(parameterIdentifier); filterExpression = ExtendedSyntaxFactory.MakeInvocation( firstArgument, firstParameterIdentifier); } for (int i = 1; i < whereArguments.Count; ++i) { ExpressionSyntax andOperand; if (whereArguments[i].IsKind(SyntaxKind.SimpleLambdaExpression)) { var currentLambda = (SimpleLambdaExpressionSyntax)whereArguments[i]; var currentParameter = currentLambda.Parameter; var currentParameterName = currentParameter.Identifier.Text; if (currentParameterName != parameterName) { var parameterSymbol = semanticModel.GetDeclaredSymbol(currentParameter); var substituteRewriter = new SubstituteRewriter( currentParameterName, parameterSymbol, semanticModel, firstParameterIdentifier); var newBody = (CSharpSyntaxNode)currentLambda.Body.Accept(substituteRewriter); andOperand = MakeExpressionFromLambdaBody(newBody); } else { andOperand = MakeExpressionFromLambdaBody(currentLambda.Body); } } else { andOperand = ExtendedSyntaxFactory.MakeInvocation( whereArguments[i], firstParameterIdentifier); } filterExpression = SyntaxFactory.BinaryExpression( SyntaxKind.LogicalAndExpression, filterExpression, andOperand); } var newLambda = SyntaxFactory.SimpleLambdaExpression( firstParameter, filterExpression); var newInvocation = ExtendedSyntaxFactory.MakeInvocation( innerMostWhereAccess, newLambda); return(newInvocation); }