0

I have a dictionary of <object, string> that I know for a fact can be converted to <string, string>. Rather than loop over the 1st dictionary to build up the second one, are there Convert methods or something else that can do this in a single call?

3
  • what exactly are you trying to achieve? what code are you trying yo write? Commented Oct 28, 2014 at 22:58
  • @ZdravkoDanev I'm trying to convert my Dictionary<object, string> to Dictionary<string, string> in as few lines of code as feasible. Commented Oct 28, 2014 at 23:03
  • 1
    @larryq - You could write an extension method that does it. Commented Oct 28, 2014 at 23:09

1 Answer 1

2

I'm not sure what do you mean by direct, if you are looking for something like List<T>.ConvertAll method, no there is no method like this for Dictionary. Even if there is you have to create a new dictionary anyway, so:

var newDic = myDictionary.ToDictionary(x => x.Key.ToString(), x => x.Value);
Sign up to request clarification or add additional context in comments.

1 Comment

I would avoid using .ToString() as it is not a "convert" or "cast" function. It could return something else.

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.