简体   繁体   中英

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.

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);

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:

水库

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM