Multi Value Dictionary?

后端 未结 10 1877
萌比男神i
萌比男神i 2020-11-27 20:45

Anyone know of a good implementation of a MultiValueDictionary? Basically, I want something that allows multiple values per key. I want to be able to do somethi

10条回答
  •  没有蜡笔的小新
    2020-11-27 21:41

    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 standard KeyValuePair and support for the IDictionary.Values property providing a collection of actual values (instead of an ICollection>).

    That way, it fits in neatly with the rest of the .NET collection classes. I also defined an IMultiDictionary interface to access operations that are particular to this kind of dictionary:

    public interface IMultiDictionary :
      IDictionary>,
      IDictionary,
      ICollection>,
      IEnumerable>,
      IEnumerable {
    
      /// Adds a value into the dictionary
      /// Key the value will be stored under
      /// Value that will be stored under the key
      void Add(TKey key, TValue value);
    
      /// Determines the number of values stored under a key
      /// Key whose values will be counted
      /// The number of values stored under the specified key
      int CountValues(TKey key);
    
      /// 
      ///   Removes the item with the specified key and value from the dictionary
      /// 
      /// Key of the item that will be removed
      /// Value of the item that will be removed
      /// True if the item was found and removed
      bool Remove(TKey key, TValue value);
    
      /// Removes all items of a key from the dictionary
      /// Key of the items that will be removed
      /// The number of items that have been removed
      int RemoveKey(TKey key);
    
    }
    

    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.

提交回复
热议问题