Flattening nested dictionaries with LINQ

假装没事ソ 提交于 2019-12-10 12:39:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!