C# Custom Dictionary Take - Convert Back From IEnumerable

Posted by Goober on Stack Overflow See other posts from Stack Overflow or by Goober
Published on 2010-03-26T15:48:19Z Indexed on 2010/03/26 15:53 UTC
Read the original article Hit count: 392

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about dictionary