3

My requirement is

Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>>

When I try to get the value of the inner Dictionary using key(outerString), it gives an error saying "cannot apply indexing with type of expression...............".

I have tried this

Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>> dict1 = new
    Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>>;

Dictionary<innerString, List<SelectListItem>> dict2 = dict1.values["outerString"];

Any quick help will be greatly appreciated.

Thx in advance.

6
  • 3
    Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>>, Good Lord!!! Commented Jan 15, 2010 at 13:58
  • 1
    This is definitely a good place to use var. Commented Jan 15, 2010 at 13:59
  • what is innerString and outerString ? Commented Jan 15, 2010 at 13:59
  • You probably just want to write String instead of outerString and innerString. Note that you have to provide types rather than variables in a type signature. Commented Jan 15, 2010 at 14:12
  • You might want to introduce a class or two to encapsulate that data structure. Nested generics is just manual code obfuscation! Commented Jan 15, 2010 at 15:17

7 Answers 7

6

I guess what you need is just:

Dictionary<innerString, List<SelectListItem>> dict = dict1["someKey"];
Sign up to request clarification or add additional context in comments.

Comments

3

You just need to change the last line of your code snippet to (I assumed where you wrote inner string and outer string you must meant string):

var dict = dict1["someValue"];

Additionally, you could probably make your code much readable with the var keyword:

var dict1 = new Dictionary<string, Dictionary<string, List<SelectListItem>>>();
var dict = dict1["someValue"];

Comments

2

So outerString and innerString are types? Did you actually just want a nested dictionary string -> string -> List<SelectListItem> ? If not, you'll have to show us the definition of these types, and give the compiler some way to convert from the string you are trying to index with...

Comments

2

You were close:

Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>> dict1 = new 
Dictionary<outerString, Dictionary<innerString, List<SelectListItem>>>(); 

// Get inner dictionary whose key is "someValue"
Dictionary<innerString, List<SelectListItem>> dict = dict1["someValue"]

Comments

1

Did I misunderstand you? This works fine

Dictionary<string, Dictionary<string, List<string>>> list = new Dictionary<string, Dictionary<string, List<string>>>();
list.Add("test", new Dictionary<string, List<string>>());
Dictionary<string, List<string>> inner = list["test"];

or

var list = new Dictionary<string, Dictionary<string, List<string>>>();
list.Add("test", new Dictionary<string, List<string>>());
Dictionary<string, List<string>> inner = list["test"];

1 Comment

Thx astander & others for quick reply I was doing list.values["test"]; But, the need was list["test"];
0

In

Dictionary<string, Dictionary<string, List<T>> dict1 = 
    new Dictionary<string, Dictionary<string, List<T>>();

you need

List<T> list12 = dict1["key1"]["key2"];

List<int> list1 = new List<int>();
list1.Add(1);
list1.Add(2);

Dictionary<string, List<int>> innerDict = new Dictionary<string, List<int>>();
innerDict.Add("inner", list1);

Dictionary<string, Dictionary<string, List<int>>> dict1 =
    new Dictionary<string, Dictionary<string, List<int>>>();
dict1.Add("outer", innerDict);

List<int> list2 = dict1["outer"]["inner"];

Comments

0

How about you use an array of strings for a key, rather than trying to nest the dictionaries -

       Dictionary<string[], List<string>> dict = 
           new Dictionary<string[],List<string>>();

       string[] key = {"inner", "outer"};
       List<string> vals = new List<string>();

       dict.Add(key, vals);

1 Comment

Dictionary relies on the semantics of the Equals and GetHashCode methods. Neither of which are implemented on arrays. In other words: var x = new string[] { "one", "two" }; var y = new string[] { "one", "two" }; Console.WriteLine("x == y? {0}", x.Equals(y)); Outputs: x == y? False

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.