Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Get Key Value from Generic Dictionary Question

Posted on 2013-06-25
3
Medium Priority
?
315 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

581 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