字典和键值对
我在使用字典时遇到问题,希望你能帮助我。
我有以下声明:
class MainCollection<TKey1, TKey2, TValue> : Dictionary<KeyValuePair<TKey1, TKey2>, TValue>
问题是我无法通过 TKey1 OR TKey2 从该字典中获取元素。 有没有办法仅通过 TKey1 OR TKey2 而不是 TKey1 AND TKey2 来获取元素?
我写了下面的代码:
public TValue GetItemByKey1(TKey1 key)
{
MainCollection<int, int, string> Coll = new MainCollection<int, int, string>();
var value = from s in Coll where s.Key.Key == key select s.Value;
}
但它已经有两个问题:
- 编译错误: s.Key.Key == key =>运算符 == 不能应用于 int 和 TKey1 类型
- 它看起来很难看。即使编译成功,我也不确定这是获取此类项目的最快方法。我想字典应该更好。
我该如何解决此类错误?我在这里没有找到任何相关问题。 提前致谢!
i have a problem with Dictionary, hope you'll help me.
I have the following declaration:
class MainCollection<TKey1, TKey2, TValue> : Dictionary<KeyValuePair<TKey1, TKey2>, TValue>
The problem is that i cant get an element from this dictionary by TKey1 OR TKey2.
Is there a way to get an element only by TKey1 OR TKey2, not TKey1 AND TKey2?
I wrote the following code:
public TValue GetItemByKey1(TKey1 key)
{
MainCollection<int, int, string> Coll = new MainCollection<int, int, string>();
var value = from s in Coll where s.Key.Key == key select s.Value;
}
But it already has two issues:
- Compilation error: s.Key.Key == key => operator == can not be applied to types int and TKey1
- It looks ugly. Even if compilation would be successful I'm not sure that this is the fastest way to get such items. I guess that Dictionary should something better.
How can i solve such errors? I didnt find any related questions here.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,您希望能够通过
TKey1
或TKey2
进行查找。那么您需要的是三个字典,一个用于每个密钥,然后一个用于密钥对。请注意,只有
(TFirstKey, TSecondKey)
的查找是唯一的,因此您需要GetByFirstKey
和GetBySecondKey
来返回集合。我会将其余的细节留给您。
要点是,如果您想快速查找任一键,则需要两个字典(一个对应键对的每个坐标)。可以通过查询键集来使用它,但这很慢(搜索键是线性的)。
Okay, so you want to be able to lookup by
TKey1
orTKey2
. Then what you want is three dictionaries, one for each of the keys, and then one for the key-pairs.Note that only lookup by
(TFirstKey, TSecondKey)
is unique, so you needGetByFirstKey
andGetBySecondKey
to return collections.I'll leave the rest of the details to you.
The point is that if you want fast lookups on either key, you need two dictionaries (one for each coordinate of the key-pair). Using one can be made to work by querying the key set, but that's slow (it's linear to search the keys).
只需向集合本身添加一个方法:
您可以为
TKey2
使用类似的方法。请注意,这些查找将比标准字典键查找慢得多,因为您正在迭代键集合,而不是利用字典本来使用的哈希表。
Just add a method to the collection itself:
You can have a similar method for
TKey2
.Note that these lookups will be much slower than a standard dictionary key lookup, since you're iterating the key collection, rather than taking advantage of the hashtable a dictionary would otherwise use.
我建议不要使用
KeyValuePair
因为 KVP 是一个结构体,并且作为字典中的键表明该对象将存在一段时间。我会推荐使用Tuple
。好处是Tuple是引用类型,可以自由传递,无需复制。此外,Tuple 是一个只读对象,就像 KVPair 一样。这是我的编写方式:注意:我包含了一个 Tuple 实现,以防您不使用 .Net 4.0
更新:
将
MainCollection
对象转换为使用多个字典将如下所示:I recommend against using
KeyValuePair<TKey, TValue>
because KVP is a struct and being a key in the dictionary indicates that the object will be around for a while. I would recommend aTuple<T1, T2>
instead. The benefit is that Tuple is a reference type and you can freely pass around without making copies. Also, Tuple is a readonly object just like the KVPair. Here's the way I would write it:NOTE: I included a Tuple implementation in case you are not using .Net 4.0
Update:
Converting the
MainCollection
object to use multiple dictionaries would look like this: