Is there a <string, object> compilation that allows multiple keys to be used?

Currently, I have a menu with subelements that are stored in this dictionary variable:

private Dictionary<string, UserControl> _leftSubMenuItems 
    = new Dictionary<string, UserControl>();

So, I am adding views for example. section "Customer":

_leftSubMenuItems.Add("customers", container.Resolve<EditCustomer>());
_leftSubMenuItems.Add("customers", container.Resolve<CustomerReports>());

But since I use the dictionary ,, I can only have one key named "clients" .

My natural tendency would be to create a custom structure with the "Section" and "View" properties, but is there a .NET compilation that is better suited for this task, something like "MultiKeyDictionary"?

Answer:

Thanks maciejkow, I expanded your offer to get exactly what I need:

using System;
using System.Collections.Generic;

namespace TestMultiValueDictionary
{
    class Program
    {
        static void Main(string[] args)
        {
            MultiValueDictionary<string, object> leftSubMenuItems = new MultiValueDictionary<string, object>();

            leftSubMenuItems.Add("customers", "customers-view1");
            leftSubMenuItems.Add("customers", "customers-view2");
            leftSubMenuItems.Add("customers", "customers-view3");
            leftSubMenuItems.Add("employees", "employees-view1");
            leftSubMenuItems.Add("employees", "employees-view2");

            foreach (var leftSubMenuItem in leftSubMenuItems.GetValues("customers"))
            {
                Console.WriteLine(leftSubMenuItem);
            }

            Console.WriteLine("---");

            foreach (var leftSubMenuItem in leftSubMenuItems.GetAllValues())
            {
                Console.WriteLine(leftSubMenuItem);
            }

            Console.ReadLine();
        }
    }

    public class MultiValueDictionary<TKey, TValue> : Dictionary<TKey, List<TValue>>
    {

        public void Add(TKey key, TValue value)
        {
            if (!ContainsKey(key))
                Add(key, new List<TValue>());
            this[key].Add(value);
        }

        public List<TValue> GetValues(TKey key)
        {
            return this[key];
        }

        public List<TValue> GetAllValues()
        {
            List<TValue> list = new List<TValue>();

            foreach (TKey key in this.Keys)
            {
                List<TValue> values = this.GetValues(key);
                list.AddRange(values);
            }

            return list;
        }
    }

}

Answer 2:

Blixt , GetAllValues ​​ :

public IEnumerable<TValue> GetAllValues()
{
    foreach (TKey key in this.Keys)
    {
        List<TValue> values = this.GetValuesForKey(key);
        foreach (var value in values)
        {
            yield return value;
        }
    }
}

2 :

, Keith:

public IEnumerable<TValue> GetAllValues()
{
    foreach (var keyValPair in this)
        foreach (var val in keyValPair.Value)
            yield return val;
}
+5
8

, Dictionary<string, List<UserControl>>? , Add, , . .

sth :

class MultiValueDictionary<TKey, TValue> : Dictionary<TKey, List<TValue>>
{

   public void Add(TKey key, TValue value)
   {
      if(!ContainsKey(key))
         Add(key, new List<TValue>());
      this[key].Add(value);
   }
}
+10

NGenerics 'HashList, , . Wintellect PowerCollections MultiDictionary, , , , .

+5

:

private Dictionary<string, List<UserControl>> _leftSubMenuItems =
    new Dictionary<string, List<UserControl>>();

if (!_leftSubMenuItems.ContainsKey("customers"))
{
    _leftSubMenuItems["customers"] = new List<UserControl>();
}
_leftSubMenuItems["customers"].Add(container.Resolve<EditCustomer>());
_leftSubMenuItems["customers"].Add(container.Resolve<CustomerReports>());
+4

...

public class MultiValueDictionary<TKey, TValue> : 
    Dictionary<TKey, List<TValue>>
{

    public void Add(TKey key, TValue value)
    {
        List<TValue> valList;
        //a single TryGetValue is quicker than Contains then []
        if (this.TryGetValue(key, out valList))
            valList.Add(value);
        else
            this.Add( key, new List<TValue> { value } );
    }

    //this can be simplified using yield 
    public IEnumerable<TValue> GetAllValues()
    {
        //dictionaries are already IEnumerable, you don't need the extra lookup
        foreach (var keyValPair in this)
            foreach(var val in keyValPair.Value);
                yield return val;
    }
}
+3

.NET Framework 3.5 LINQ Lookup.

, . , , , .

, - , .

:

var example1 = (from element in ListWithDuplicates
            select element)
           .ToLookup(A => A.Name);

:

  • Lookup , Lookup, , .ToLookup.
  • , ..
  • ,

, "" .

+3

, . , " " , "", , Dictionary, . ( , ):

Dictionary<string, Dictionary<Type, object>> dict = ...;
var value = (T)dict[name][typeof(T)];

, -. , , , Dictionary.

+1

I do not know "MultiKeyDictionary". I would recommend using a structure and overriding GetHashCode, Equals and implementing IEquatable <StructName> (which is used by Dictionary <TKey, TValue>).

0
source

Do you want to save several records on one key together? Something like this ?

0
source

All Articles