简体   繁体   中英

How would I store this data?

I have some data that goes over three levels, like this:

Identifier, Key, Value

I would like to first look up the Identifier. Let's say it's 1. Then I would look up the key, let's say 2. Finally, the key would give me the value.

I guess I might need multi-value dictionaries or some two/three dimensional arrays. All the data is fixed, with only 6 elements needed for an array.

So, I would like something in code that can be accessed like this:

SomeInfo[identifier, key] which would return value.

Please ask for more info.

Use the Tuple class, and make the Tuple your key. That way your key can be composed of multiple values:

SomeInfo[new Tuple<string, string>(identifier, key)];

To elaborate, the Tuple class provides a proper implementation of GetHashCode() and Equals() that respects the values you pass in. And if "identifier" or "key" are not string, you can change the type arguments you pass into it.

Edit : Here's a working example:

var dictionary = new Dictionary<Tuple<string, string>, string>();
dictionary[new Tuple<string, string>("myIdentifier", "myKey")] = "myValue";

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