类型字典列表 [英] list of type dictionary

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

问题描述


我想创建类型字典的列表,我必须使用key
从该列表(字典类型)中检索/读取数据 例如:


I want to create list of type dictionary and i have to retrive/read the data from that list(dictionary type) by using key
ex:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Populate example Dictionary
        var dict = new Dictionary<string,string>();
        dict.Add("name", "dinakar");
        dict.Add("city", "delhi");

        // Get a List of all the Keys
        List<string> keys = new List<string>(dict.Keys);
    }
}



请如何通过比较key = value格式告诉我如何从List<dict>中读取值.
请帮我



please how tell me how to read values from List<dict> by comparing key=value format.
please help me out

推荐答案

根据您的问题,这是我的建议:

According to your question, this is what I recommend:

List<string> keylist = new List<string>();
foreach (Dict<string, string> dict in list)
{
    keyList.AddRange(dict.Keys);
}



但是,您要执行的操作没有任何意义,因为即使您现在有了列表中字典的键列表,您也不再知道哪个dict对象包含给定键.此外,您打算如何处理来自不同dict对象的多个具有相同名称的键的可能性?



However, what you want to do makes no sense because even though you now have a list of keys from the dictionaries in your list, you no longer know which dict object contains a given key. Further, how do you propose to handle the possibility of multiple keys with the same name from different dict objects?


从我的代码可以看出,该列表是不必要的.

From what I can tell from your code, the list shouldnt be necessary.

public string GetMyValue(string key)
{
if (dict.ContainsKey(key))
    return dict[key];
else
    return null; 
}


尝试
foreach( KeyValuePair<string, string> kvp in dict )
{
keys.add(kvp.key);
}


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

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