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?
-
what exactly are you trying to achieve? what code are you trying yo write?Z .– Z .2014-10-28 22:58:01 +00:00Commented 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.larryq– larryq2014-10-28 23:03:43 +00:00Commented Oct 28, 2014 at 23:03
-
1@larryq - You could write an extension method that does it.ChaosPandion– ChaosPandion2014-10-28 23:09:57 +00:00Commented Oct 28, 2014 at 23:09
Add a comment
|
1 Answer
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);
1 Comment
Enigmativity
I would avoid using
.ToString() as it is not a "convert" or "cast" function. It could return something else.