遞歸樹

 

        /// <summary>
        /// 遞歸獲取樹
        /// </summary>
        /// <param name="parentId">父節點ID</param>
        /// <param name="originList">原始數據</param>
        /// <returns></returns>
        public List<Bd_ProductCategory_Tree_ViewModel> GetBdProductCategoryTreeViewList(int parentId, List<Bd_ProductCategory_ViewModel> originList)
        {
            List<Bd_ProductCategory_Tree_ViewModel> treeList = new List<Bd_ProductCategory_Tree_ViewModel>();
            List<Bd_ProductCategory_ViewModel> list = originList.Where(s => s.ParentId == parentId).ToList();
            foreach (var item in list)
            {
                Bd_ProductCategory_Tree_ViewModel bdProductCategoryTreeViewModel = new Bd_ProductCategory_Tree_ViewModel();
                if (!string.IsNullOrEmpty(item.ProductCategoryName))
                {
                    bdProductCategoryTreeViewModel.Key = item.ProductCategoryName;
                    bdProductCategoryTreeViewModel.Title = item.ProductCategoryName;
                }

                List<Bd_ProductCategory_Tree_ViewModel> childList = GetBdProductCategoryTreeViewList(item.Id, originList);
                if (childList != null && childList.Count > 0)
                {
                    bdProductCategoryTreeViewModel.Children = childList;
                }
                treeList.Add(bdProductCategoryTreeViewModel);

            }
            return treeList;
        }
public class Bd_ProductCategory_Tree_ViewModel
    {
        /// <summary>
        /// 標題
        /// </summary>
        [JsonProperty("Title")]
        public string Title { get; set; }
        /// <summary>
        /// 關鍵詞
        /// </summary>
        [JsonProperty("Key")]
        public string Key { get; set; }
        /// <summary>
        /// 子品類
        /// </summary>
        [JsonProperty("Children")]
        public List<Bd_ProductCategory_Tree_ViewModel> Children { get; set; }
    }
相關文章
相關標籤/搜索