File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Original file line number Diff line number Diff 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 ) ;
You can’t perform that action at this time.
0 commit comments