从字典到列表 [英] From Dictionary to List

查看:67
本文介绍了从字典到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是字典.

This is Dictionary.

Dictionary<string, List<string>> Dic = new Dictionary<string, List<string>>();



我想这样做-我单击按钮,然后将第一个Dic键的值复制到List< string>.再次单击-下一个Dic键及其值等.



I want to do so - i clicking button and the first Dic key with her values copied to List<string>. Click again - next Dic Key and her values and etc..

推荐答案

请按照以下说明操作

适用于Framework 2.0

Do as per below

for framework 2.0

foreach ( var item in dicNumber)  {     
  listnumber.Add (item.Key);   
} 



使用LINQ




using LINQ


var Listname= dictionary.Keys.ToList(); 



检查tis是否能解决您的问题,谢谢



check if tis solve your problem ,thanks


我有个主意,如何一次获得所有这些:
I have an idea how to get them all at once:
public static void Example()
{
    Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
    dic.Add(
        "first",
        new List<string>(new string[] { "first first", "second first" })
    );
    dic.Add(
        "second",
        new List<string>(new string[] {
            "first second", "second second", "third second"
        })
    );

    List<string> output = new List<string>();

    foreach (string key in dic.Keys)
    {
        output.AddRange(dic[key]);
    }
}


对于一对一的解决方案,您需要为每次按钮单击指定含义.为此尝试System.Windows.Forms.ListBox.

但是,结果列表将在旁边包含键和值.除非密钥以某种特殊方式格式化,否则您将无法再将它们区分开.


For a one-by-one solution, you would need to specify for each button click, which key you meant. Try a System.Windows.Forms.ListBox for that.

However, the resulting list will contain keys and values alongside. Unless the keys are formatted in some special way, you won''t be able to distinguish one from another anymore.


这篇关于从字典到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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