Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting result of Dictionary of objects after Skip and Take

Tags:

c#

linq

I have a Dictionary of objects defined like this -

Dictionary<string, MyObject> myObjectDictionary 

I would like to grab items in the Dictionary based of an index and count of items. I am trying to use Skip and Take. But this requires recasting it back to Dictionary<string, MyObject>. How can this be done? Is there a different way I should be doing this?

Here is my code and failed attempt to recast -

Dictionary<string, MyObject> myObjectDictionary = FillMyObjectDictionary();

var mySmallerObjectDictionary = myObjectDictionary.Skip(startNumber).Take(count);

//THE FOLLOWING DOES NOT WORK
Dictionary<string, MyObject> myNewObjectDictionary = (Dictionary<string, MyObject>)mySmallerObjectDictionary  
like image 271
A Bogus Avatar asked Jan 07 '14 21:01

A Bogus


People also ask

What is TKey and TValue in C#?

The Dictionary<TKey, TValue> Class in C# is a collection of Keys and Values. It is a generic collection class in the System. Collections. Generic namespace. The Dictionary <TKey, TValue> generic class provides a mapping from a set of keys to a set of values.

What is dictionary object in C#?

Dictionary is a collection of keys and values in C#. Dictionary is included in the System. Collection. Generics namespace.

Which among the below options is the correct way to get the values in dictionary in C#?

foreach loop: You can use foreach loop to access the key/value pairs of the dictionary.As shown in the below example we access the Dictionary using a foreach loop.

Is dictionary reference type C#?

It is a class hence it is a Reference Type.


2 Answers

Well, you can create a new dictionary:

Dictionary<string, MyObject> myNewObjectDictionary =
    myObjectDictionary.Skip(startNumber)
                      .Take(count)
                      .ToDictionary(pair => pair.Key, pair => pair.Value);

However:

  • You shouldn't rely on the ordering of a dictionary. It's not clear which items you wish to skip. Consider using OrderBy before Skip. For example:

    Dictionary<string, MyObject> myNewObjectDictionary =
        myObjectDictionary.OrderBy(pair => pair.Key)
                          .Skip(startNumber)
                          .Take(count)
                          .ToDictionary(pair => pair.Key, pair => pair.Value);
    
  • This does not preserve any custom equality comparer that was in the original dictionary. Unfortunately that's not exposed anywhere, so you'll just have to know whether or not FillMyObjectDictionary uses a custom comparer.

like image 94
Jon Skeet Avatar answered Sep 22 '22 15:09

Jon Skeet


The result of the Skip / Take operation will not be an IDictionary<string, MyObject>, it will be an IEnumerable<KeyValuePair<string, MyObject>>.

If you want to convert this back to a dictionary, try calling ToDictionary:

var myNewObjectDictionary = mySmallerObjectDictionary.ToDictionary(p => p.Key, p => p.Value); 
like image 35
p.s.w.g Avatar answered Sep 23 '22 15:09

p.s.w.g