C#递归模型,你处于哪个级别。

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

c# Recursive model with what level you are on

问题

Sure, here's the translated code portion:

这是您要的翻译部分:

原始代码部分:

public IEnumerable<Model> GetItems(string rootLink) {
    foreach (var node1 in loadItems(rootLink)) {
       yield return new Model(node1, level: 1);

       foreach (var node2 in loadItems(node1.link)) {
           yield return new Model(node2 , level: 2);  
 
           foreach (var node3 in loadItems(node2.link)) {
               yield return new Model(node3 , level: 3);  
           }
       }
    }
}

尝试使用递归的代码部分:

public IEnumerable<Model> GetItems(string link, int level = 1)
{
    foreach (var node in loadItems(link))
    {
        yield return new Model(node, level);

        foreach (var subNode in GetItems(node.link, level + 1))
        {
            yield return subNode;
        }
    }
}

希望这对您有所帮助。如果您需要更多帮助,请随时告诉我。

英文:

Say you have a simplified tree like this

  • Level 1 item
    • Level 2 item
    • Level 2 item
      • Level 3 item
  • Level 1 item
  • Level 1 item
    • Level 2 item
      • Level 3 item
      • Level 3 item
    • Level 2 item

I have this code which works fine and loops through it and creates a list with all the items and which level they are on. But this will only work with 3 levels and it would be nice if this could work with any levels and less repeated code.

public IEnumerable&lt;Model&gt; GetItems(string rootLink) {
    foreach (var node1 in loadItems(rootLink) {
       yield return new Model(node1, level: 1);

       foreach (var node2 in loadItems(node1.link) {
           yield return new Model(node2 , level: 2);  
 
           foreach (var node3 in loadItems(node2.link) {
               yield return new Model(node3 , level: 3);  
           }
       }
    }
}

So I am wondering if I can make this a recursive function instead. I tried with something like this, but the level will be wrong here since I am just adding to it.

public IEnumerable&lt;Model&gt; GetItems(string link, int level = 1)
{
    foreach (var node in loadItems(link))
    {
        yield return new Model(category, level);

        foreach (var subNode in GetItems(category.link, level++))
        {
            yield return subNode;
        }
    }
}

答案1

得分: 1

以下是翻译好的代码部分:

public IEnumerable<Model> GetItems(string rootLink) 
{
    // 设置工作队列
    var queue = new Queue<Item>();
    // 从根节点开始
    foreach(var level1 in loadItems(rootLink))
        queue.Enqueue(new Item(level1, 1));

    while(queue.Count > 0)
    {
        var current = queue.Dequeue();
        yield return new Model(current.Node, current.Level);

        // 如果当前节点有子节点,则将其加入队列
        foreach( var child in loadItems(current.Node.link))
            queue.Enqueue(new Item(child, current.Level+1);
    }
}

private record Item(YourType Node, int Level);

希望这有所帮助!

英文:

Untested, but an iterative approach would look something like this:

public IEnumerable&lt;Model&gt; GetItems(string rootLink) 
{
    // Setup a work queue
    var queue = new Queue&lt;Item&gt;();
    // Start with the root node
    foreach(var level1 in loadItems(rootLink))
        queue.Enqueue(new Item(level1, 1));

    while(queue.Count &gt; 0)
    {
        var current = queue.Dequeue();
        yield return new Model(current.Node, current.Level);

        // Enqueue child-nodes of the current node if any
        foreach( var child in loadItems(current.Node.link))
            queue.Enqueue(new Item(child, current.Level+1);
    }
}

private record Item(YourType Node, int Level);

huangapple
  • 本文由 发表于 2023年5月10日 18:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217364.html
匿名

发表评论

匿名网友

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

确定