82

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?

6 Answers 6

131

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());
}
Sign up to request clarification or add additional context in comments.

13 Comments

I am using .NET 2.0, is possible in it. I am not getting "ToDictionary"
@Rune: Will k.Value.ToString() return always the same as (string)k.Value?
@Oscar: No. the (string)k.Value will fail if the value is a non-string object. The ToString() is safe for all non-null values.
@Rune: Yes, I know that. Let's suppose all elements are strings. Will ToString() and (string) return the same?
Might I suggest using Convert.ToString() instead. So it will be a little easier to deal with null values. Convert.ToString(keyValuePair.Value) or Convert.ToString(keyValuePair.Value) ?? String.Empty
|
9

.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));

Comments

4

Since you are using .net 2.0:

Dictionary<string, string> dString = new Dictionary<string, string>();

foreach (KeyValuePair<string, object> item in dObject)
{
    dString.Add(item.Key, item.Value.ToString());
    //here item.Value.ToString() is an example
    //you should convert your item to string according to the requirement
}

3 Comments

What if there is NULL values in it?
@MKS: That's why I'm saying "convert your item to string according to the requirement", for example you can use string.Empty for null values, or the string "NULL" for null values...everything depends on the requirement.
Sometime there can be null values also in the key, please suggest how to convert it?
2

How about:

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

5 Comments

This will fail if the dictionary contains non-string values. You cannot convert an int to a string for instance.
First of all I am using .NET 2.0, is possible in it. I am not getting "ToDictionary" method.
Well, I suppose if he wants to do it is because the dictionary contains strings, but anyway, that's valid.
Yeah true.. But ToString does not always produce a nice string. It all depends on your Dictionary objects anyhow. :)
Since you are using .NET 2.0, foreach will be your bestest buddy! GL
1

In .Net 2.0 probably your best friend is foreach loop:

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

foreach (KeyValuePair<string, object> keyValuePair in dObject)
{
    newObjects.Add(keyValuePair.Key, keyValuePair.Value.ToString());
}

Comments

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

It's just idea. You can insert any verifications.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.