Compiler error when using LINQ in IEnumerable <dynamic>, but not the first time IEnumerable <dynamic>
OK, so I'm writing some really dirty code, since the library I'm working with returns dynamic type hierarchies. You can expand some of these types into lists of dynamic types and enable working with these dynamic object hierarchies in LINQ. I wrote a small method that basically converts some dynamic objects to IEnumerable <dynamic>.
I have this method that returns an IEnumerable <dynamic> but when I try to use it with LINQ, I get the error "I can not use a lambda expression as an argument for a dynamically sent operation without first including it in the delegate or tree tree type expressions. " However, if I pass methods the return value from IEnumerable <dynamic> to IEnumerable <dynamic> (no-op in my mind), it compiles and works fine.
Can someone 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.";
}
The result Foo(unknown)is equal dynamic, not IEnumerable<dynamic>. This is because the call is Fooresolved dynamically because unknown- dynamic.
Foo , object unknown = new ExpandoObject(); Foo((object) unknown).
, , dynamic. IEnumerable<T> , T dynamic, # using, , dynamic, , . .Select(value => value) , , Func<dynamic, dynamic> projection = value => value;, unknown.Select(projection) (untested), . Enumerable.Select(unknown, projection), .