简体   繁体   English

如何从C#中的嵌套并发字典获取值到列表中

[英]How to get values into list from nested concurrent dictionary in c#

I have a nested concurrent dictionary as given below: 我有一个嵌套的并发字典,如下所示:

ConcurrentDictionary<string,ConcurrentDictionary<string,<Class Object>>>

I want to get all objects (values of inner dictionary) into list for further processing without knowing any key. 我想将所有对象(内部字典的值)放入列表中以进行进一步处理,而无需知道任何键。

I tried below two solutions but it does not work for me, 我尝试了以下两种解决方案,但不适用于我,

  1. outer dictionary.Values.Select(x=> x.Values) 外部dictionary.Values.Select(x=> x.Values)
  2. foreach loop foreach循环

The problem with first solution is that it won't give only objects and second solution is time consuming. 第一个解决方案的问题是它不会只提供对象,第二个解决方案很耗时。

If you run dictionary.Values.Select(x=> x.Values) you would not get a list of object values from the inner dictionaries; 如果运行dictionary.Values.Select(x=> x.Values) ,则不会从内部字典中获得对象值的列表; you will get a list of lists of object values. 您将获得对象值列表的列表

To "flatten" that list, use SelectMany : 要“拉平”该列表,请使用SelectMany

foreach (var inner  in dictionary.Values.SelectMany(x=> x.Values)) {
    ...
}

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

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