C# Custom Dictionary Take - Convert Back From IEnumerable
- by Goober
Scenario
Having already read a post on this on the same site, which didn't work, I'm feeling a bit stumped but I'm sure I've done this before.
I have a Dictionary.
I want to take the first 200 values from the Dictionary.
CODE
Dictionary<int,SomeObject> oldDict = new Dictionary<int,SomeObject>();
//oldDict gets populated somewhere else.
Dictionary<int,SomeObject> newDict = new Dictionary<int,SomeObject>();
newDict = oldDict.Take(200).ToDictionary();
OBVIOUSLY, the take returns an IENumerable, so you have to run ToDictionary() to convert it back to a dictionary of the same type. HOWEVER, it just doesn't work, it wants some random key selector thing - or something? I have even tried just casting it but to no avail.
Any ideas?