字典<字符串,MyClass的>列出< KeyValuePair<字符串,字符串>>其中KeyValuePair值是MyClass.ToString()

字典<字符串,MyClass的>列出< KeyValuePair<字符串,字符串>>其中KeyValuePair值是MyClass.ToString()

问题描述:

Dictionary<string, MyClass> dict = new Dictionary<string, MyClass>();

//where MyClass has an override for ToString()

现在怎么样做我收到了列表&LT; KeyValuePair&LT;字符串,字符串&GT;&GT; 从字典其中KeyValuePair值,如果 MyClass.ToString(),并在KeyValuePair的关键是相同的字典..?

Now how do I get a List<KeyValuePair<string, string>> from dict where the Value in KeyValuePair if MyClass.ToString() and the Key in the KeyValuePair is same as that of dict..?

有一个简单的方法来做到这一点?我如何使用 IDictionary.ToList&LT;&GT;()的功能呢? PLZ开导..

Is there an easy way to do that? How can I use the IDictionary.ToList<>() function there? Plz enlighten ..

没有测试/编译,但是这样的事情应该工作:

Not tested/compiled, but something like that should work:

dict.Select(kvp => new KeyValuePair<string, string>(kvp.Key, kvp.Value.ToString())).ToList()

如果语法是不是100%正确的,我希望你有这个想法。

if the syntax is not 100% spot on, I hope you got the idea.