Skip to content

Commit c918327

Browse files
committed
Code review changes
1 parent 010cf85 commit c918327

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/NHibernate/Linq/Visitors/HqlGeneratorExpressionVisitor.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,14 @@ protected HqlTreeNode VisitUnaryExpression(UnaryExpression expression)
526526
case ExpressionType.Convert:
527527
case ExpressionType.ConvertChecked:
528528
case ExpressionType.TypeAs:
529-
var notCastable = _notCastableExpressions.TryGetValue(expression, out var castType);
530-
castType = castType ?? expression.Type;
529+
var castable = !_notCastableExpressions.TryGetValue(expression, out var castType);
530+
if (castable)
531+
{
532+
castType = expression.Type;
533+
}
531534

532-
return IsCastRequired(expression.Operand, castType, out var existType) && !notCastable
533-
? _hqlTreeBuilder.Cast(VisitExpression(expression.Operand).AsExpression(), expression.Type)
535+
return IsCastRequired(expression.Operand, castType, out var existType) && castable
536+
? _hqlTreeBuilder.Cast(VisitExpression(expression.Operand).AsExpression(), castType)
534537
// Make a transparent cast when an IType exists, so that it can be used to retrieve the value from the data reader
535538
: existType && HqlIdent.SupportsType(castType)
536539
? _hqlTreeBuilder.TransparentCast(VisitExpression(expression.Operand).AsExpression(), castType)

src/NHibernate/Linq/Visitors/NhPartialEvaluatingExpressionVisitor.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,7 @@ public override Expression Visit(Expression expression)
8080
if (expression.NodeType == ExpressionType.Lambda || !_partialEvaluationInfo.IsEvaluatableExpression(expression) ||
8181
#region NH additions
8282
// Variables should be evaluated only when they are part of an evaluatable expression (e.g. o => string.Format("...", variable))
83-
expression is UnaryExpression unaryExpression &&
84-
(
85-
ExpressionsHelper.IsVariable(unaryExpression.Operand, out _, out _) ||
86-
// Check whether the variable is casted due to comparison with a nullable expression
87-
// (e.g. o.NullableShort == shortVariable)
88-
unaryExpression.Operand is UnaryExpression subUnaryExpression &&
89-
unaryExpression.Type.UnwrapIfNullable() == subUnaryExpression.Type &&
90-
ExpressionsHelper.IsVariable(subUnaryExpression.Operand, out _, out _)
91-
)
92-
)
83+
ContainsVariable(expression))
9384
#endregion
9485
return base.Visit(expression);
9586

@@ -162,8 +153,27 @@ private Expression EvaluateSubtree(Expression subtree)
162153
}
163154
}
164155

156+
#region NH additions
157+
158+
private bool ContainsVariable(Expression expression)
159+
{
160+
if (!(expression is UnaryExpression unaryExpression))
161+
{
162+
return false;
163+
}
164+
165+
return ExpressionsHelper.IsVariable(unaryExpression.Operand, out _, out _) ||
166+
// Check whether the variable is casted due to comparison with a nullable expression
167+
// (e.g. o.NullableShort == shortVariable)
168+
unaryExpression.Operand is UnaryExpression subUnaryExpression &&
169+
unaryExpression.Type.UnwrapIfNullable() == subUnaryExpression.Type &&
170+
ExpressionsHelper.IsVariable(subUnaryExpression.Operand, out _, out _);
171+
}
172+
165173
#endregion
166-
174+
175+
#endregion
176+
167177
protected override Expression VisitConstant(ConstantExpression expression)
168178
{
169179
if (expression.Value is Expression value)

0 commit comments

Comments
 (0)