Rohit
Rohit

Reputation: 10236

How to get value from Dictionary<string,object>

I have a dictionary for which I want to retrieve results matching to those of list

here is what I have done so far

         Dictionary<string, Dictionary<string, int>> SomeDictionary = new Dictionary<string, Dictionary<string, int>>();
        List<int> MyList = new List<int>()
        {

            2,3,4,5
        };

        Dictionary<string, int> internalDictionary = new Dictionary<string, int>();
        internalDictionary.Add("two", 2);
        internalDictionary.Add("three", 3);
        internalDictionary.Add("four", 4);
        internalDictionary.Add("five", 5);

        Dictionary<string, int> AnotherDictionary = new Dictionary<string, int>();
        AnotherDictionary.Add("six", 6);
        AnotherDictionary.Add("three", 3);
        AnotherDictionary.Add("seven", 7);


        SomeDictionary.Add("Dictionary1", internalDictionary);
        SomeDictionary.Add("Dictionary2", AnotherDictionary);


        var res = from l in MyList
                  select(from q in
                  (from p in
                       (from s in SomeDictionary
                        select s)
                   select p) where q.Value.Equals(l) select q);

The value returned is null. what am i Missing ?

I need matching KeyValuePair where value matches internal dictionary values.

Upvotes: 0

Views: 2471

Answers (2)

vrluckyin
vrluckyin

Reputation: 1024

Explanation:

  1. Select Many to combine all internal dictionaries into All In One dictionary.
  2. List and SelectMany are IEnumerable. So join is possible between to IEnumerable objects.
  3. However, List contains string value while IEnumerable object returned from SelectMany has integer value.

So created inner join query with string and integer value after converting integer to string. Refer screencast that might be required output

Screen cast showing working code

This Linq snippet may be helpful:

var allinone = (from l in MyList
                       join d in SomeDictionary.SelectMany(s => s.Value) on l equals d.Value
                       select d);

Upvotes: 3

Enigmativity
Enigmativity

Reputation: 117029

Try this:

    var res = from l in MyList
              from q in SomeDictionary
              from w in q.Value
              where w.Value == l
              select w;

I get this:

res

Upvotes: 1

Related Questions