转换嵌套的foreach循环用3水平复发n级 [英] Convert nested foreach-loops with 3 levels to recurrence with n levels
本文介绍了转换嵌套的foreach循环用3水平复发n级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想转换下面的代码来处理超过3的foreach的水平。
I want to convert the following code to handle more than 3 foreach-levels.
internal static void CreateLevel(LevelObject levelObject)
{
foreach(LevelObject l1 in levelObject.LevelObjects)
{
foreach(LevelObject l2 in l1.LevelObjects)
{
foreach(LevelObject l3 in l2.LevelObjects)
{
AddEntities(l3);
}
AddEntities(l2);
}
AddEntities(l1);
}
}
每个LevelObject有子级的对象的集合,实体的集合和一个父级对象。
我需要这一个对象的结构转换为一个过滤器。
任何人有一个想法如何转换这种
Every LevelObject has a collection of child-level-objects, a collection of entities and a parent-level-object. I need this to convert an object-structure to a filter. Anyone has an idea how to convert this?
推荐答案
最简单的方法是递归的:
The easiest way is to recurse:
internal static void CreateLevel(LevelObject levelObject) {
foreach (var l in levelObject.LevelObjects) {
CreateLevel(l);
AddEntities(l);
}
}
这篇关于转换嵌套的foreach循环用3水平复发n级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文