多值字典?
有人知道 MultiValueDictionary 的良好实现吗?基本上,我想要一个允许每个键有多个值的东西。我希望能够执行类似的操作
dict.Add(key, val);
,如果该键尚不存在,它将添加它,如果存在,它只会向该键添加另一个值。我只是要迭代它,所以我并不关心其他检索方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Microsoft 刚刚添加了您正在寻找的正式预发布版本(称为
MultiValueDictionary
),可通过 NuGet 获取:https://www.nuget.org/packages/Microsoft.Experimental.Collections/有关使用信息和更多详细信息可以通过官方 MSDN 博客文章找到这里: http: //blogs.msdn.com/b/dotnet/archive/2014/06/20/would-you-like-a-multidictionary.aspx
我是此包的开发人员,因此请在此处告诉我如果您对性能或任何其他问题有任何疑问,请访问 MSDN。
希望有帮助。
Microsoft just added an official prelease version of exactly what you're looking for (called a
MultiValueDictionary
) available through NuGet here: https://www.nuget.org/packages/Microsoft.Experimental.Collections/Info on usage and more details can be found through the official MSDN blog post here: http://blogs.msdn.com/b/dotnet/archive/2014/06/20/would-you-like-a-multidictionary.aspx
I'm the developer for this package, so let me know either here or on MSDN if you have any questions about performance or anything.
Hope that helps.
您可以轻松地从列表字典中创建一个:
You can easily make one from a dictionary of lists:
它不存在,但您可以从 Dictionary 和 List 快速构建一个:
It doesn't exist, but you can build one pretty quickly from Dictionary and List:
您始终可以使用 Tuple 作为第二个通用参数:
或者甚至,通用列表作为第二个通用参数:
这将允许您将多个值绑定到单个键。
为了便于使用,您可以创建一个扩展方法来检查密钥是否存在以及列表中的添加内容。
You can always use a Tuple for your second generic parameter:
Or even , a generic List as a second generic parameter:
That will allow you to bind multiple values to a single key.
For ease of use, you can create an extension method that will check for existence of a key and addition to the list.
这是我不久前写的,你可以使用。
它有一个继承自Dictionary的“MultiValueDictionary”类。
它还具有一个扩展类,允许您在值类型为 IList 的任何字典上使用特殊的添加功能;这样,如果您不愿意,就不会被迫使用自定义类。
Here's one I wrote a while back that you can use.
It has a "MultiValueDictionary" class that inherits from Dictionary.
It also has an extension class that allows you to use the special Add functionality on any Dictionary where the value type is an IList; that way you're not forced to use the custom class if you don't want to.
只是为了将我的 0.02 美元添加到解决方案集合中:
早在 2011 年,我就有了同样的需求,并创建了一个
MultiDictionary
,其中包含所有 .NET 接口的迂腐完整实现。其中包括返回标准KeyValuePair
的枚举器,并支持提供实际值集合的IDictionary.Values
属性(而不是ICollection>
)。这样,它就可以与其他 .NET 集合类完美契合。我还定义了一个
IMultiDictionary
接口来访问此类字典特有的操作:它可以在 .NET 2.0 以上的任何版本上进行编译,到目前为止我已将其部署在Xbox 360、Windows Phone 7、Linux 和 Unity 3D。还有一个完整的单元测试套件,涵盖了每一行代码。
该代码根据 Common Public License 获得许可(简而言之:一切都可以,但错误修复库的代码必须发布)并且可以在 我的 Subversion 存储库中找到。
Just to add my $0.02 to the collection of solutions:
I had the same need back in 2011 and created a
MultiDictionary
with a pedantically complete implementation of all the .NET interfaces. That includes enumerators that return a standardKeyValuePair<K, T>
and support for theIDictionary<K, T>.Values
property providing a collection of actual values (instead of anICollection<ICollection<T>>
).That way, it fits in neatly with the rest of the .NET collection classes. I also defined an
IMultiDictionary<K, T>
interface to access operations that are particular to this kind of dictionary:It can be compiled on anything from .NET 2.0 upwards and so far I've deployed it on the Xbox 360, Windows Phone 7, Linux and Unity 3D. There's also a complete unit test suite covering every single line of the code.
The code is licensed under the Common Public License (short: anything goes, but bug fixes to the library's code have to published) and can be found in my Subversion repository.
您可以使用 PowerCollections 中的 MultiDictionary 类。
它返回所请求的键的 ICollection{TValue}。
You could use MultiDictionary class from PowerCollections.
It returns ICollection{TValue} for the key asked.
然而这里是我使用
ILookup
的尝试和一个内部KeyedCollection
。确保关键属性是不可变的。交叉发布于此处。
Yet here's my attempt using
ILookup<TKey, TElement>
and an internalKeyedCollection
. Make sure the key property is immutable.Cross posted here.
目前应该这样做...
This ought to do for now...
自定义类型的替代方案可以是通用扩展,它在找不到时添加键和值:
示例使用:
Alternative to custom type can be a generic extension that adds key and value when not found:
Sample use: