繁体   English   中英

如何从字典中获得价值<string,object>

[英]How to get value from Dictionary<string,object>

我有一本字典,我想检索与列表匹配的结果

这是我到目前为止所做的

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

返回的值为空。 我想念什么?

我需要匹配KeyValuePair ,其中值匹配内部字典值。

说明:

  1. 选择“许多”可将所有内部词典合并为“多合一”词典。
  2. List和SelectMany是IEnumerable的。 因此,可以在IEnumerable对象之间进行联接。
  3. 但是,List包含字符串值,而从SelectMany返回的IEnumerable对象具有整数值。

因此,在将整数转换为字符串后,使用字符串和整数值创建了内部联接查询。 请参阅可能需要输出的截屏视频

屏幕截图显示了工作代码

这个Linq片段可能会有所帮助:

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

尝试这个:

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

我得到这个:

水库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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