Solved

Get Key Value from Generic Dictionary Question

Posted on 2013-06-25
3
310 Views
Last Modified: 2013-07-12
Hello all,

I have in MVC this "RouteValueDictionary" and in this it has 3 key value pairs such as this:

Keys:
[0] Data
[1] Total
[2] Errors

Then under Values I have the following if I click on Values:
[0] Count = 1
[1] 1
[2] Count = 1

If I expand the [2] I see:
[0] = {[key, System.Collections.Generic.Dictionary`2[System.String,System.Object]]}

Then under that I see:
Key: "key"
Value: Count = 1

Then I see when I expand the value:
[0] = {[errors, System.String[]]}

Then again I expand the [0] I see:
Key = "errors"
Value = {string[1]}

Then Finally I see what I am trying to get at under value expanded:
[0] = "Error Message"

How do I get to this key value?
0
Comment
Question by:sbornstein2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 39276924
Dictionary<string, string> dic = new Dictionary<string, string>();
        dic["A"] = "Ahmed";
        dic["B"] = "Boys";
        foreach (string mk in dic.Keys)
        {
            if(dic[mk]=="Ahmed")
                Console.WriteLine("key contain \"Ahmed\" is "+mk);
        }
0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39277323
Hi,

You could also try LINQ. First of all i create the dictionary, which you already have:

	Dictionary<string,object> d = new Dictionary<string,object>();
	d.Add("Data",VRtblCinema.ToDictionary (x => x.Cinema_strCode,x => x.Cinema_strNameLegal));
	d.Add("Total",1);
	d.Add("Errors",new string[] {"error1","error2"});

Open in new window


Then with LINQ you select the key you want, and store it in a string array:
	string[] a = (string[])d.Where(x=>x.Key=="Errors").Select (x => x.Value).FirstOrDefault ();
	foreach(var s in a)
	{
		Console.WriteLine(s);
	}

Open in new window


The FirstOrDefault ensures that you will get something.

Then you may deal with the error array as you may, in my case i just output it to the console.

Giannis
0
 
LVL 13

Expert Comment

by:jonnidip
ID: 39277925
You can try this:
            Dictionary<String, Object> test = new Dictionary<String, Object>();
            Object value = null;
            if (test.TryGetValue("myKey", out value))
                Console.WriteLine("myKey exists.");

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question