Cannot convert anonymous type to expression tree because it contains a field that is used in the initialization of another field

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

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  
    
    

Anonymous Types
Expression Trees
How to: Use Expression Trees to Build Dynamic Queries

Show: