问题
So I have a dictionary of the form Dictionary<int, Dictionary<int, Object>> myObjects
and I would like to flatten this to a List<Object> flattenedObjects
as simply as possible. I tried to come up with a clever solution, but so far all I've gotten to work is a solution with two nested foreach -loops that iterate over all of the elements, but I suppose there should be a nicer way of accomplishing this with LINQ.
回答1:
try this
List<Object> flattenedObjects = myObjects.Values.SelectMany(myObject => myObject.Values).ToList();
回答2:
Like this:
var result = myObjects.Values.SelectMany(d => d.Values).ToList();
来源:https://stackoverflow.com/questions/8415196/flattening-nested-dictionaries-with-linq