简体   繁体   中英

Looking for an alterantive to List<KeyValuePair<string, KeyValuePair<string, string>>>

Ended up with this awful data structure:

List<KeyValuePair<string, KeyValuePair<string, string>>>

It's not likely to get huge (<1K I estimate) and I am gonna iterate this list over and over.

Anyone can think of some better alternative with built-in types?

struct MrStruct
{
   public string Key1,
   public string Key2,
   public string Value1
}


List<MrStruct>;

This is assuming that you are accessing the list sequentially as you did say iterate over. Potentially, other data structures could be faster for searching.

The best option would be to wrap your own Tuple class, sort of like the one shipping in .NET 4.0 .

Then you could have a single:

List<Tuple<string,string,string>>

This is easy enough to write in .NET 2.0 - it's basically just a triplet of values, instead of having 2 in a KeyValuePair. There is no built-in equivelent for a triplet of values in .NET 2.0, though.


Edit:

After reading your comment about querying in another post, I thought I'd mention this as well -

Even if you don't have unique values in key1, you could dramatically speed up any type of query/search by using:

Dictionary<string, List<KeyValuePair<string,string>>>

Then, instead of storing a single KeyValuePair, you could look up the list of them via the key in the first element. This would be much, much faster if you needed to find all of the elements with a given first key...

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