Cannot convert anonymous type to expression tree because it contains a field that is used in the initialization of another field
The compiler does not accept conversion of an anonymous to an expression tree when one property of the anonymous type is used to initialize another property of the anonymous type. For example, in the following code, Prop1 is declared in the initialization list and then used as the initial value for Prop2.
Module M2
Sub ExpressionExample(Of T)(ByVal x As Expressions.Expression(Of Func(Of T)))
End Sub
Sub Main()
' The following line causes the error.
' ExpressionExample(Function() New With {.Prop1 = 2, .Prop2 = .Prop1})
End Sub
End Module
Error ID: BC36548
To correct this error
Assign the initial value for Prop1 to a local variable. Assign that variable to both Prop1 and Prop2, as shown in the following code.
Sub Main() Dim temp = 2 ExpressionExample(Function() New With {.Prop1 = temp, .Prop2 = temp}) End Sub
See Also
Tasks
How to: Use Expression Trees to Build Dynamic Queries