locked
C#2010 An expression tree may not contain a dynamic operation

    Question

  • I understand the text of the error and what is occurring in my code - I have a dynamic type defined as a parameter to a method. I try to use that type within a Linq statement
    e.g. - 
    var query =
    from row in Db.SomeTable
    where row.SomeField == dynamicType.SomeProperty
    select row

    Any idea on how to code around this?

    For background, I am trying to migrate an app to .NET v4 from 'some other language' and do not want to rearchitect the app during the conversion (that's v2<g>).
    Thursday, May 28, 2009 9:35 PM

All replies

  • Could you store the value in a temp value just above the query?

    string value = dynamicType.SomeProperty

    var query =
    from row in Db.SomeTable
    where row.SomeField == value
    select row
    David Morton - http://blog.davemorton.net/ - @davidmmorton
    Thursday, May 28, 2009 9:37 PM
    Moderator
  • I would have a huge amount of variables as the methods I am looking at have dozens of properties being used.
    e.g. -
    object column1 = row.SomeField1
    object column2 = row.SomeField2
    object column3 = row.SomeField3
    object column4 = row.SomeField4
    ...

    And then you need to understand and try to cast to the appropriate type - worse because many of these are complex objects, not a simple int. My example probably should have been something more like:
    var query =
    from row in Db.SomeTable
    where row.SomeField == dynamicType.SomeInnerClass.SomeProperty
    select row

    Thursday, May 28, 2009 9:42 PM