Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Dictionary<string, object> to Dictionary<string, string> in c#

Tags:

c#

c#-2.0

I have below code in C#

Dictionary<string, object> dObject = new Dictionary<string, object>(); 

I want to convert dObject to Dictionary<string, string>. How can I do this?

like image 294
Manoj Singh Avatar asked Feb 09 '11 08:02

Manoj Singh


People also ask

How do you turn an object into a dictionary?

I found it easy to json serialize the object and deserialize as a dictionary. var json = JsonConvert. SerializeObject(obj); var dictionary = JsonConvert. DeserializeObject<Dictionary<string, string>>(json);

Can a dictionary key be a string in C#?

Add elements to a C# Dictionary Both types are generic so it can be any . NET data type. The following Dictionary class is a generic class and can store any data type. This class is defined in the code snippet creates a dictionary where both keys and values are string types.

What is dictionary string string in C#?

In C#, Dictionary is a generic collection which is generally used to store key/value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of Dictionary is, it is generic type. Dictionary is defined under System. Collection.

Can dictionary store different data types?

One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary.


2 Answers

Use the ToDictionary method:

Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value.ToString()); 

Here you reuse the key from the original dictionary and you convert the values to strings using the ToString method.

If your dictionary can contain null values you should add a null check before performing the ToString:

Dictionary<string, string> dString = dObject.ToDictionary(k => k.Key, k => k.Value == null ? "" : k.Value.ToString()); 

The reason this works is that the Dictionary<string, object> is actually an IEnumerable<KeyValuePair<string,object>>. The above code example iterates through the enumerable and builds a new dictionary using the ToDictionary method.

Edit:

In .Net 2.0 you cannot use the ToDictionary method, but you can achieve the same using a good old-fashioned foreach:

Dictionary<string, string> sd = new Dictionary<string, string>();  foreach (KeyValuePair<string, object> keyValuePair in dict) {   sd.Add(keyValuePair.Key, keyValuePair.Value.ToString()); } 

Edit2:

If you are on .Net 2.0 and you can have null values in the dictionary the following should be safe:

Dictionary<string, string> sd = new Dictionary<string, string>();  foreach (KeyValuePair<string, object> keyValuePair in dict) {   sd.Add(keyValuePair.Key, keyValuePair.Value == null ? "" : keyValuePair.Value.ToString()); } 
like image 60
Rune Grimstad Avatar answered Oct 17 '22 20:10

Rune Grimstad


.NET 3.5:

Dictionary<string, string> result = dObject.ToDictionary(kvp => kvp.Key, kvp => Convert.ToString(kvp.Value)); 

.NET 2.0:

Dictionary<string, string> result = new Dictionary<string, string>(); foreach (KeyValuePair<string, object> kvp in dObject) result.Add(kvp.Key, Convert.ToString(kvp.Value)); 
like image 29
Jaroslav Jandek Avatar answered Oct 17 '22 20:10

Jaroslav Jandek