简体   繁体   English

Ilookup 接口与 IDictionary

[英]ILookup interface vs IDictionary

How does the ILookup<key, value> interface differ from IDictionary<key, value> ? ILookup<key, value>接口与IDictionary<key, value>有何不同?

I don't understand what the ILookup interface is meant for.我不明白ILookup接口是什么意思。

ILookup entries can contain multiple items per key - each key is mapped to an IEnumerable<TElement> . ILookup条目可以包含每个键的多个项目 - 每个键都映射到一个IEnumerable<TElement>

Also as hinted to in the comments an ILookup is immutable, while you can update values in an IDictionary (it exposes an Add() method and an indexer that allows getting and setting values).同样如注释中所暗示的, ILookup是不可变的,而您可以更新IDictionary中的IDictionary (它公开了一个Add()方法和一个允许获取设置值的索引器)。

In summary their use case is very different - you use a lookup when you need a 1:N map with values that are fixed and won't (and can't) change.总而言之,它们的用例非常不同 - 当您需要具有固定值且不会(也不能)更改的 1:N 映射时,您可以使用查找。 A dictionary on the other hand offers a mutable 1:1 mapping of key value pairs, so it can be updated to add or remove values.另一方面,字典提供键值对的可变1:1 映射,因此可以更新它以添加或删除值。

It is much more simpler than IDictionary .它比IDictionary简单得多。 It is used by Linq.它由 Linq 使用。 It only has Contains , Item and Count .它只有ContainsItemCount IDictionary has Add , Remove , etc. IDictionaryAddRemove等。

ILookUp => Group by key , Enumerable Collection ILookUp => 按键分组,可枚举集合

Single key value refers to enumerable collection where we can iterate through the value collection.单键值指的是可枚举集合,我们可以在其中迭代值集合。

IDictionary => Group by distinct key , Single value IDictionary => 按不同键分组,单值

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

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