I understand that anonymous types cannot be returned from methods. I also understand that there are workaround ways of doing this. My question is, what method do Linq providers use? For example, (cue dim understanding of how Linq providers work), the Select() extension method is applied to an IQueryable, whose expression tree is parsed by the Linq provider, turned into SQL, and the query results are parsed, loaded into a type and then…. returned.
So what technique to get Select() to return the type is used by e.g. Entity Framework or NHibernate?
It uses generics. Through type inference, the compiler knows the exact type of the anonymous type.
Please note that the compiler automatically generates a class for anonymous types. After that, it is just a type like any other type.
You can return an anonymous type from your methods. The problem is, that you can’t do that strongly typed, because the type name of the anonymous type is not known to you – but it is to the compiler, that’s why generics work with anonymous types.
The workaround you are talking about is also using generics.