简体   繁体   English

寻找List <KeyValuePair <string,KeyValuePair <string,string >>>的替代品

[英]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. 它不太可能变得很大(我估计<1K)并且我将一遍又一遍地重复这个列表。

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 . 最好的选择是包装你自己的Tuple类,有点像.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. 这很容易用.NET 2.0编写 - 它基本上只是一个三元组的值,而不是KeyValuePair中的2。 There is no built-in equivelent for a triplet of values in .NET 2.0, though. 但是,.NET 2.0中的三元组值没有内置等效。


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: 即使您在key1中没有唯一值,也可以使用以下方法显着加快任何类型的查询/搜索:

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. 然后,您可以通过第一个元素中的键查找它们的列表,而不是存储单个KeyValuePair。 This would be much, much faster if you needed to find all of the elements with a given first key... 如果您需要使用给定的第一个键找到所有元素,这将会更快,更快...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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