I ran across an unexpected problem with the following code.
List<string> items = new List<string>();
items = items.OrderBy(item => item);
This code generates the error:
Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)
It appears I can change items
to be of type IEnumerable<string>
and the error goes away. But I need to be able to add items to the list, which IEnumerable
doesn't support.
Can someone help me understand this error, and what the easiest fix is? Is it safe to simply cast the result?
See Question&Answers more detail:os