I have a collection class that inherits from List<> . I created a function to sort the list using a specific property:
public PlaylistCollection SortByName(IEnumerable<Playlist> playlists) { return (PlaylistCollection)playlists.OrderBy(p => p.Name); }
When I try to use sorted results in my code, for example:
artistSource.Playlists = (PlaylistCollection)new List<Playlist>(artistSource.Playlists.SortByName(artistSource.Playlists));
I get an error message:
Unable to cast object of type 'System.Linq.OrderedEnumerable`2[...Playlist,System.String]' to type '...PlaylistCollection'."
This is moderately frustrating given that VS told me that there was an explicit conversion, so I added the above listing.
How to properly drop from IEnumerable<> to my collection?
generics casting c # linq
Eric Smith
source share