-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code follows the simplification ReLinq did for us. Resolves #17791
- Loading branch information
Showing
5 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/EFCore/Query/Internal/InvocationExpressionRemovingExpressionVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq.Expressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Query.Internal | ||
{ | ||
public class InvocationExpressionRemovingExpressionVisitor : ExpressionVisitor | ||
{ | ||
protected override Expression VisitInvocation(InvocationExpression invocationExpression) | ||
{ | ||
var invokedExpression = StripTrivialConversions(invocationExpression.Expression); | ||
|
||
return invokedExpression is LambdaExpression lambdaExpression | ||
? InlineLambdaExpression(lambdaExpression, invocationExpression.Arguments) | ||
: base.VisitInvocation(invocationExpression); | ||
} | ||
|
||
private Expression StripTrivialConversions(Expression expression) | ||
{ | ||
while (expression is UnaryExpression unaryExpression | ||
&& unaryExpression.NodeType == ExpressionType.Convert | ||
&& expression.Type == unaryExpression.Operand.Type | ||
&& unaryExpression.Method == null) | ||
{ | ||
expression = unaryExpression.Operand; | ||
} | ||
|
||
return expression; | ||
} | ||
|
||
private Expression InlineLambdaExpression(LambdaExpression lambdaExpression, ReadOnlyCollection<Expression> arguments) | ||
{ | ||
var replacements = new Dictionary<Expression, Expression>(arguments.Count); | ||
for (var i = 0; i < lambdaExpression.Parameters.Count; i++) | ||
{ | ||
replacements.Add(lambdaExpression.Parameters[i], arguments[i]); | ||
} | ||
|
||
return new ReplacingExpressionVisitor(replacements).Visit(lambdaExpression.Body); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters