字典<字符串,MyClass的>列出< KeyValuePair<字符串,字符串>>其中KeyValuePair值是MyClass.ToString() [英] Dictionary<string, MyClass> to List<KeyValuePair<string, string>> where the Value in KeyValuePair is MyClass.ToString()

查看:105
本文介绍了字典<字符串,MyClass的>列出< KeyValuePair<字符串,字符串>>其中KeyValuePair值是MyClass.ToString()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这篇关于字典&LT;字符串,MyClass的&GT;列出&LT; KeyValuePair&LT;字符串,字符串&GT;&GT;其中KeyValuePair值是MyClass.ToString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆