Yes, you can select an anonymous type, but you cannot select an anonymous type, and then use these objects as if they were implementing an interface. The reason is that the anonymous type does not implement the interface, although they have the same properties, both in the type and in the name. There is no way to determine that an anonymous type implements an interface. If you want to use objects through your interface implementations, you need to select a specific type that implements this interface.
Anonymous types are class types that are derived directly from an object and cannot be passed to any type other than an object. The compiler provides a name for each anonymous type, although your application cannot access it. In terms of common language runtime, an anonymous type is no different from any other reference type.
Link: http://msdn.microsoft.com/en-us/library/bb397696.aspx
I can, of course, sympathize with your intentions here. It would be nice if the compiler could understand that the type corresponds to the definition of the interface when the type is anonymous, but this is really only applicable when the interface consists entirely of read-only properties. The moment your interface defines properties using setters or methods, an anonymous type cannot implement it. On the other hand, if you use the anonymous type as you see fit - as a short-term temporary type for a particular use - you can simply reference its properties and you don't need an interface at all.
var query = from n in names.AsQueryable() select new {A = n.ToUpper(), B = 2012}; foreach (var item in query) { Console.WriteLine( item.A ); }
source share