问题描述
Dictionary<string, MyClass> dict = new Dictionary<string, MyClass>();
//where MyClass has an override for ToString()
现在怎么样做我收到了列表< KeyValuePair<字符串,字符串>>
从字典其中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<>()
的功能呢? 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.
这篇关于字典<字符串,MyClass的>列出< KeyValuePair<字符串,字符串>>其中KeyValuePair值是MyClass.ToString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!