If I use this next bellow code in C# using Unity, I get a IEnumerable<widget>
back but I want an ObservableCollection<widget>
back.
public class widget
{
public string myString;
public int myInt;
}
public ObservableCollection<widget> myCollectionOfWidgets;
I can iterate through the collection with ForEach with no problem. I even implemented IComparable for my widget so that I can sort them with myInt property.
But when I try to get the first 15 items in the collection ordered by myInts in descendingOrder, I get an IEnumerable back:
var newWidgetCollection = myCollectionOfWidgets.OrderByDescending(myCollectionOfWidgets => myCollectionOfWidgets.myInts).Take(15);
If I try to cast it back to an ObservableCollection of widgets like so:
var newObservableCollectionOfWidgets = (ObservableCollection<widget>)newWidgetCollection;
I get an error reading this:
"InvalidCastException: Specified cast is not valid."
I would like someone to answer me:
- Why does this happen? In other words, I forgot to use an interface, which one?
- Maybe point me in the correct direction.
- If my code is incorrect, where did I go wrong.