使用LINQ展平嵌套字典 [英] Flattening nested dictionaries with LINQ

查看:58
本文介绍了使用LINQ展平嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个格式为Dictionary<int, Dictionary<int, Object>> myObjects的字典,我想尽可能简单地将其展平为List<Object> flattenedObjects.我试图提出一个聪明的解决方案,但是到目前为止,我要做的只是一个带有两个嵌套的foreach循环的解决方案,该循环遍历所有元素,但是我想应该有一种更好的方法来完成此任务LINQ.

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.

推荐答案

尝试一下

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

这篇关于使用LINQ展平嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆