Solved

Get Key Value from Generic Dictionary Question

Posted on 2013-06-25
3
312 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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