从项目中递归获取项目,更改它,并将其添加到某处。

huangapple go评论58阅读模式
英文:

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.
    }

huangapple
  • 本文由 发表于 2023年4月17日 23:15:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036674-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定