如何展平对象集合(对象集合又包含集合)?
我有一个 IEnumerable 集合,它是分层的,因为一个元素包含多个元素。因此,如果我进行计数,我可能会得到 7-8 作为返回 int,而实际上可能有 500 个项目(因为它们是嵌套的)。
如何将此集合展平为包含所有元素且无嵌套的集合?
谢谢
I have an IEnumerable collection, which is hierarchical in that one element contains several within it. Thus, if I do a count, I may get 7-8 as the return int, when really there could be 500 items (as they're nested).
How can I flatten this collection into a collection with all the elements and no nesting?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
smallEnumerable
是包含 7-8 个项目的集合,其中每个项目都有一个属性SubItems
,它本身就是相同类型项目的可枚举,那么您可以像这样展平如果每个
SubItems
都可以拥有SubItems
本身,那么就可以进行一些递归:Assuming that
smallEnumerable
is the collection with 7-8 items, each one of which has a propertySubItems
which is itself an enumerable of items of the same type, then you flatten like this:If each one of the
SubItems
can haveSubItems
itself, then some recursion is in order: