Skip to content

Commit 2d806b0

Browse files
committed
Cleanup code
1 parent 6621c5e commit 2d806b0

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/NHibernate/Linq/DmlExpressionRewriter.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,25 @@ public static Expression PrepareExpressionFromAnonymous<TSource>(Expression sour
122122
throw new ArgumentNullException(nameof(expression));
123123

124124
// Anonymous initializations are not implemented as member initialization but as plain constructor call, potentially wrapped in a Convert expression
125-
var newExpression = expression.Body as NewExpression ??
126-
(expression.Body is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.Convert && unaryExpression.Operand is NewExpression newExpressionOperand
127-
? newExpressionOperand
128-
: throw new ArgumentException("The expression must be an anonymous initialization, e.g. x => new { Name = x.Name, Age = x.Age + 5 }"));
125+
var newExpression = UnwrapConvert(expression.Body) as NewExpression ??
126+
throw new ArgumentException("The expression must be an anonymous initialization, e.g. x => new { Name = x.Name, Age = x.Age + 5 }");
129127

130128
var instance = new DmlExpressionRewriter(expression.Parameters);
131129
instance.AddSettersFromAnonymousConstructor(newExpression, "");
132130
return PrepareExpression<TSource>(sourceExpression, instance._assignments);
133131
}
134132

133+
private static Expression UnwrapConvert(Expression expression)
134+
{
135+
if (expression is UnaryExpression unaryExpression &&
136+
unaryExpression.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked)
137+
{
138+
return unaryExpression.Operand;
139+
}
140+
141+
return expression;
142+
}
143+
135144
public static Expression PrepareExpression<TSource>(Expression sourceExpression, IReadOnlyDictionary<string, Expression> assignments)
136145
{
137146
var lambda = ConvertAssignmentsToBlockExpression<TSource>(assignments);

0 commit comments

Comments
 (0)