将字典转换为列表集合在C# [英] Convert dictionary to list collection in C#

查看:795
本文介绍了将字典转换为列表集合在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例如果我有字典与模板字符串作为键和字符串作为值。然后我希望将字典键转换为列表集合作为字符串。

 字典< string,string> dicNumber = new Dictionary< string,string>(); 
列表< string> listNumber = new List< string>();

dicNumber.Add(1,First);
dicNumber.Add(2,Second);
dicNumber.Add(3,Third);

//所以代码可能看起来像这样
// listNumber = dicNumber.Select(??????);


解决方案

将密钥转换为自己的列表:

  listNumber = dicNumber.Select(kvp => kvp.Key).ToList(); 

或者您可以缩短,甚至不用使用选择:

  listNumber = dicNumber.Keys.ToList(); 


I have problem when trying to convert dictionary to list.

Example if I have dictionary with template string as key and string as value. Then i wish to convert dictionary key to list collection as string.

Dictionary<string, string> dicNumber = new Dictionary<string, string>();
List<string> listNumber = new List<string>();

dicNumber.Add("1", "First");
dicNumber.Add("2", "Second");
dicNumber.Add("3", "Third");

// So the code may something look like this
//listNumber = dicNumber.Select(??????);

解决方案

To convert the Keys to a List of their own:

listNumber = dicNumber.Select(kvp => kvp.Key).ToList();

Or you can shorten it up and not even bother using select:

listNumber = dicNumber.Keys.ToList();

这篇关于将字典转换为列表集合在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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