Smoothing Nested Dictionaries Using LINQ

So, I have a dictionary of the form Dictionary<int, Dictionary<int, Object>> myObjects , and I would like to reduce it to List<Object> flattenedObjects as simple as possible. I tried to come up with a smart solution, but so far all that I have started working is a solution with two nested foreach -loops that iterate over all the elements, but I suppose there should be a better way to accomplish this using LINQ.

+7
source share
2 answers

try it

 List<Object> flattenedObjects = myObjects.Values.SelectMany(myObject => myObject.Values).ToList(); 
+11
source

Like this:

 var result = myObjects.Values.SelectMany(d => d.Values).ToList(); 
+7
source

All Articles