英文:
recursively get item from items change it and add somewhere
问题
在这些代码中,我可以获取到groupItem,但是每个groupItem都有一个Items属性,而每个item又再次有一个Items属性。你可以如何修改这些代码,使其递归工作?
// 递归处理组和项的方法
void ProcessGroupAndItems(ControlInfoDataGroup group, IEnumerable<ItemInfo> items)
{
foreach (var itemInfo in items)
{
bool isNew = itemInfo.IsNew;
bool isUpdated = itemInfo.IsUpdated;
bool isPreview = itemInfo.IsPreview;
// ...
var item = new ControlInfoDataItem(itemInfo.UniqueId, ...);
item.IncludedInBuild = isIncludedInBuild;
foreach (var doc in itemInfo.Docs)
{
item.Docs.Add(new ControlInfoDocLink(doc.Title, doc.Uri));
}
foreach (var relatedControl in itemInfo.RelatedControls)
{
item.RelatedControls.Add(relatedControl);
}
group.Items.Add(item);
}
}
// 递归处理组的方法
void ProcessGroups(IEnumerable<GroupItem> groups)
{
foreach (var groupItem in groups)
{
var infoBadgeGroup = GetInfoBadge(groupItem.InfoBadge);
ControlInfoDataGroup group = new ControlInfoDataGroup(groupItem.Title, ...);
ProcessGroupAndItems(group, groupItem.Items);
Groups.Add(group);
}
}
// 调用递归方法开始处理
ProcessGroups(controlInfoDataGroup.Groups);
这些代码将递归处理组和项,以便正确处理嵌套的结构。
英文:
in this codes, i can get groupItem, but every groupitem has a Items property and every item has an Items property again. how i can change this codes to recursively work?
foreach (var groupItem in controlInfoDataGroup.Groups)
{
var infoBadgeGroup = GetInfoBadge(groupItem.InfoBadge);
ControlInfoDataGroup group = new ControlInfoDataGroup(groupItem.Title,...);
foreach (var itemInfo in group.Items)
{
bool isNew = itemInfo.IsNew;
bool isUpdated = itemInfo.IsUpdated;
bool isPreview = itemInfo.IsPreview;
...
var item = new ControlInfoDataItem(itemInfo.UniqueId,
...);
item.IncludedInBuild = isIncludedInBuild;
foreach (var doc in itemInfo.Docs)
{
item.Docs.Add(new ControlInfoDocLink(doc.Title, doc.Uri));
}
foreach (var relatedControl in itemInfo.RelatedControls)
{
item.RelatedControls.Add(relatedControl);
}
group.Items.Add(item);
}
Groups.Add(group);
}
答案1
得分: 0
I will give you a direction. Instead of this:
foreach (var groupItem in controlInfoDataGroup.Groups)
{
var infoBadgeGroup = GetInfoBadge(groupItem.InfoBadge);
Make a method like this:
public void ProcessGroupItems(Item groupItem, ..){ // Transfer the relevant arguments that are relevant for the next iteration.
if(groupItem.Item ==null){
//if it is void, dont execute the logic that comes after the closing if statement, if you have return type, then return from the method.
}
// Execute all your loop logic.
}
英文:
I will give you a direction. Instead of this:
foreach (var groupItem in controlInfoDataGroup.Groups)
{
var infoBadgeGroup = GetInfoBadge(groupItem.InfoBadge);
Make a method like this:
public void ProcessGroupItems(Item groupItem, ..){ // Transfer the relevant arguments that are relevant for the next iteration.
if(groupItem.Item ==null){
//if it is void, dont execute the logic that comes after the closing if statement, if you have return type, then return from the method.
}
// Execute all your loop logic.
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论