简体   繁体   中英

How can I get the inner list value of list of keyvaluepair of (string, list)?

I am now struggling to find a way to get inner list from complicated List of keyvalupair.

public static List<KeyValuePair<string, List<KeyValuePair<int, string>>>> conditions = new List<KeyValuePair<string, List<KeyValuePair<int, string>>>>();

I want list from above list using given string value as a key.

Can anyone help me please?

You can use FirstOrDefault method like this:

conditions.FirstOrDefault(x => x.Key == "key").Value;

Or you should probably use a Dictionary instead.

You could use a Dictionary and a Dictionary as your value as well.

public static Dictionary<string, Dictionary<int, string>> conditions = new Dictionary<string, Dictionary<int, string>>();

and access it with conditions[key]

For example:

//Initialize random, pointless dictionary for example
var conditions = new Dictionary<string, Dictionary<int, string>>
{
    {
        "firstDict", new Dictionary<int, string>
        {
            {1, "blue"},
            {2, "red"}
        }
    },
    {
        "secondDict", new Dictionary<int, string>
        {
            {1, "car"},
            {2, "truck"}
        }
    }
};

//Get dictionary with key value "firstDict"
var firstDict = conditions["firstDict"];
//Gets the value associated with key "1"
var color = firstDict[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM