Multi Value Dictionary?

后端 未结 10 1874
萌比男神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:46

    This ought to do for now...

    public class MultiValueDictionary : IEnumerable>
    {
        private Dictionary> _dict = new Dictionary>();
    
        public void Add(TKey key, TValue value)
        {
            if(!_dict.ContainsKey(key)) _dict[key] = new LinkedList();
            _dict[key].AddLast(value);
        }
    
        public IEnumerator> GetEnumerator()
        {
            foreach (var list in _dict)
                foreach (var value in list.Value)
                    yield return new KeyValuePair(list.Key, value);
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

提交回复
热议问题