OK, so I am writing some really messy code as the library I am working with is returning dynamic type hierarchies. Some of these types can be unfolded to lists of dynamic types and to enable me to work with these dynamic object hierarchies in LINQ I wrote a little method which basically transforms some dynamic objects to an IEnumerable<dynamic>.
I have this method that returns an IEnumerable<dynamic> but when I try to use it with LINQ I get the error "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.", however if I cast the methods return value from IEnumerable<dynamic> to IEnumerable<dynamic> (a no-op in my mind), it compiles and works fine.
Can anybody explain this behavior to me?
void Main()
{
Foo(null).Select(value => value); // OK... I was expecting this to work.
dynamic unknown = new ExpandoObject();
Foo(unknown).Select(value => value); //COMPILER ERROR: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type... this was a bit more unexpected.
((IEnumerable<dynamic>)Foo(unknown)).Select(value => value); // OK... this was really unexpected.
}
IEnumerable<dynamic> Foo(dynamic param)
{
yield return "Tranformation logic from param to IEnumerable of param goes here.";
}