Solved

Get Key Value from Generic Dictionary Question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

937 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now