如何中断asp.net中的递归函数 [英] How To Break Recursive function in asp.net

查看:66
本文介绍了如何中断asp.net中的递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在下面给出了代码.
我想在检查数据库中的值后退出循环...

public void load_tree()
{
   DataSet PrSet = PDataset("Select * from categories where idparentcategory =1");
   TreeView1.Nodes.Clear();
   foreach (DataRow dr in PrSet .Tables [0].Rows)
   {
      if ((int)dr["idparentcategory"] == 1)
      {
         TreeNode tnParent = new TreeNode();
         tnParent.Text = dr["categoryDesc"].ToString().Trim();
         tnParent.Value = dr["idCategory"].ToString().Trim();
         tnParent.Expand();
         TreeView1.Nodes.Add(tnParent);
         FillChild(tnParent, tnParent.Value);
       }
    }
}

public int FillChild(TreeNode parent, string idCategory)
{
   DataSet ds = PDataset("Select * from categories where idparentcategory =" + idCategory);
   if (ds.Tables[0].Rows.Count > 0)
   {
      foreach (DataRow dr in ds.Tables[0].Rows)
      {
         TreeNode child = new TreeNode();
         child.Text = dr["categoryDesc"].ToString().Trim();
         string temp = dr["idCategory"].ToString();
         child.Collapse();
         //parent.ChildNodes.Add(child);
         //TreeView1.Nodes.Add(child);
         parent.ChildNodes.Add(child);
         FillChild(child, temp);
      }
      return 0;
    }
    else
    {
       return 0;
    }
}

解决方案

有一个break;关键字用于循环.你是这个意思吗?但是,您的代码并未以这种方式一次又一次查询数据库的方式进行优化.寻找一种查询一次所需数据并使用业务逻辑进行填充的方法.递归也比for循环昂贵. >声明您的代码,可以吗?

如果不是,那您想做什么呢?


Hello,

I have given code below.
I want To exit the loop after checking the value in database...

public void load_tree()
{
   DataSet PrSet = PDataset("Select * from categories where idparentcategory =1");
   TreeView1.Nodes.Clear();
   foreach (DataRow dr in PrSet .Tables [0].Rows)
   {
      if ((int)dr["idparentcategory"] == 1)
      {
         TreeNode tnParent = new TreeNode();
         tnParent.Text = dr["categoryDesc"].ToString().Trim();
         tnParent.Value = dr["idCategory"].ToString().Trim();
         tnParent.Expand();
         TreeView1.Nodes.Add(tnParent);
         FillChild(tnParent, tnParent.Value);
       }
    }
}

public int FillChild(TreeNode parent, string idCategory)
{
   DataSet ds = PDataset("Select * from categories where idparentcategory =" + idCategory);
   if (ds.Tables[0].Rows.Count > 0)
   {
      foreach (DataRow dr in ds.Tables[0].Rows)
      {
         TreeNode child = new TreeNode();
         child.Text = dr["categoryDesc"].ToString().Trim();
         string temp = dr["idCategory"].ToString();
         child.Collapse();
         //parent.ChildNodes.Add(child);
         //TreeView1.Nodes.Add(child);
         parent.ChildNodes.Add(child);
         FillChild(child, temp);
      }
      return 0;
    }
    else
    {
       return 0;
    }
}

解决方案

There is a break; key word there for loops. Is that what you mean? However your code is not optimized in such a way querying the database again and again. Find a way to query the required data once and use business logic to fill. Also recursion is costly than for loops.


I''m not exactly sure what you are asking: it can''t be as simple as "add a return or a break statement to your code, can it?

If it isn''t, what are you trying to do, that is not happening?


这篇关于如何中断asp.net中的递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆