使用yield进行递归

public partial class Controls_SectionIndex : System.Web.UI.UserControl
{
    private NBearLite.Database Db = NBearLite.Database.Default;

    protected void Page_Load(object sender, EventArgs e)
    {
        int sectionType = Convert.ToInt32(Request.QueryString["sectiontype"]);
        rep.DataSource = ListIndex(sectionType, 0);
        rep.DataBind();

    }


    public IEnumerable<GHSection> ListIndex(int sectionType, int parentID)
    {
        List<SqlGHSection> list = Db.Select(Tables.GHSection)
            .Where(Tables.GHSection.SectionType == sectionType && Tables.GHSection.ParentID == parentID)
            .OrderBy(Tables.GHSection.ID.Asc)
            .ToList<SqlGHSection>();
        foreach (GHSection node in list)
        {
            yield return node;
            foreach (GHSection node3 in ListIndex(sectionType, node.ID))
            {
                yield return node3;
            }

        }
    }
}

posted @ 2008-06-20 11:18  永不言败  阅读(1099)  评论(0编辑  收藏  举报