Click here to Skip to main content
14,239,386 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi,
I want to create list of type dictionary and i have to retrive/read the data from that list(dictionary type) by using key
ex:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Populate example Dictionary
        var dict = new Dictionary<string,string>();
        dict.Add("name", "dinakar");
        dict.Add("city", "delhi");

        // Get a List of all the Keys
        List<string> keys = new List<string>(dict.Keys);
    }
}


please how tell me how to read values from List<dict> by comparing key=value format.
please help me out
Posted
Updated 20-Jul-11 7:17am
v3
Comments
Sergey Alexandrovich Kryukov 21-Jul-11 11:17am
   
Not clear what you want to achieve and why.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 4

According to your question, this is what I recommend:

List<string> keylist = new List<string>();
foreach (Dict<string, string> dict in list)
{
    keyList.AddRange(dict.Keys);
}


However, what you want to do makes no sense because even though you now have a list of keys from the dictionaries in your list, you no longer know which dict object contains a given key. Further, how do you propose to handle the possibility of multiple keys with the same name from different dict objects?
   
v4
Comments
DominicZA 20-Jul-11 13:29pm
   
Yip, which is exactly why I proposed my solution!
#realJSOP 20-Jul-11 18:11pm
   
*According to his question* that's not what he wants. He said he wanted all of the keys in all of the dictionaries in his list of dictionaries.
fcronin 20-Jul-11 14:21pm
   
My 5... dominic, you don't take into account any of what John says here...the issue is, the OP appears to be viewing his requirements in the wrong light, as the dictionary does not seem to be utilized properly in his case. If name and city are properties of an object, he would be better off either using a List<object> (where object has properties of name, city, etc), or using a Dictionary<string, object=""> if he wants to key off of a unique value for each entity.
fcronin 20-Jul-11 14:22pm
   
(the word 'object' got scrubbed from Dictionary<> in my last comment...)
Rate this:
Please Sign up or sign in to vote.

Solution 1

From what I can tell from your code, the list shouldnt be necessary.

public string GetMyValue(string key)
{
if (dict.ContainsKey(key))
    return dict[key];
else
    return null; 
}
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Try
foreach( KeyValuePair<string, string> kvp in dict )
{
keys.add(kvp.key);
}
   
Comments
DominicZA 20-Jul-11 13:18pm
   
Im not 100% sure about what this guys is trying to achieve, but if he wanted to populate the List with the keys couldnt he just say keys = dict.keys.ToList();
Kind of like he has done?
Rate this:
Please Sign up or sign in to vote.

Solution 3

Take a look at the public TValue this[TKey key] property http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]

string value = dict[keys[0]];


Gets you the value of the first key ...

Regards
Espen Harlinn
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100