Solved

Get Key Value from Generic Dictionary Question

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
comparing string in C# 3 33
Expando 4 34
Re-position sub-options beneath the TAB 7 41
Asp.net mvc 5 5 17
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 …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

19 Experts available now in Live!

Get 1:1 Help Now