递归遍历树结构顺序显示并且添加到list集合


 /// <summary>
        /// 给流程排序 by 于連偉 2015/06/15
        /// </summary>
        /// <param name="lstNextItem">本集合的下一个集合</param>
        /// <param name="returnItem">排序后的返回集合</param>
        public static void OrderLink(List<TLink> lstNextItem, List<TLink> returnItem) 
        {
            foreach (TLink link in lstNextItem)
            {
                if (link.LinkState != "E")
                {
                    if (returnItem.Find(x => { return x.LinkGuid == link.LinkGuid; }) == null)
                    {
                        returnItem.Add(link);
                    }
                }
            }
            foreach (TLink link in lstNextItem)
            {
                if (link.LinkState != "E")
                {
                    List<TLink> newLink = ListNextLink(link.LinkGuid);
                    OrderLink(newLink, returnItem);
                }
            }
        }

        public static List<TLink> ListNextLink(string linkGuid)
        {
            using (OracleConnection conn = new OracleConnection(TDataCore.mJkConnStr))
            {
                try
                {
                    List<TLink> lst = new List<TLink>();
                    conn.Open();

                    OracleCommand cmd = conn.CreateCommand();
                    cmd.CommandText = string.Format("select {0} from t_link where linkGuid in (select endLinkGuid from t_line where startLinkGuid=:linkGuid)", m_LinkField);
                    OracleParameter param = cmd.Parameters.Add(":linkGuid", OracleDbType.Varchar2);
                    param.Value = linkGuid;
                    using (OracleDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            lst.Add(GetLink(dr));
                        }
                    }
                    conn.Close();
                    return lst;
                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
        }

 

 

其中ListNextLink方法是查找下一个节点的方法,要依据实际情况自己去写出

这个递归是第一个和最后一个元素不递归,中间的每个元素都要查找出下一个元素然后循环去添加每一个递归出来的本元素的下一个元素,如果重复则不添加

posted @ 2015-06-16 09:05  洛晨随风  阅读(622)  评论(0编辑  收藏  举报