public List<NhomSanPham2> get_tree(NhomSanPham root=null, int level=0)
 {
     this._tmp_for_get_tree = new List<NhomSanPham2>();
     //để dành level cho root
     if (root != null)
     {
         level++;
     }
     List<NhomSanPham2> tmp = this._get_tree(root, level);
     this._tmp_for_get_tree = new List<NhomSanPham2>();
     //nếu root!=null thì add root vào trước tiên
     if (root != null)
     {
         NhomSanPham2 root_ = new NhomSanPham2();
         root_.Load_From(root);
         root_.level = level-1;
         tmp.Insert(0,root_);
     }
     return tmp;
 }
        public ActionResult Index(int page = 1,int id_loaisp=0,int level_loaisp=0)
        {
            ViewBag.id = 2;
            NhomSanPhamController ctr2 = new NhomSanPhamController(ctr._db);
            List<NhomSanPham> loaisp_list = new List<NhomSanPham>();
            NhomSanPham2 loaisp = new NhomSanPham2();
            List<List<SanPham>> splist = new List<List<SanPham>>();
            if (id_loaisp == 0 || level_loaisp == 0 )
            {
                NhomSanPham tmp;
                if (id_loaisp != 0)
                {
                    tmp = ctr2.get_by_id(id_loaisp);
                    loaisp.Load_From(tmp);
                    loaisp.level = level_loaisp;
                    if (tmp.ds_nhomcon.Count == 0)
                    {
                        loaisp_list.Add(tmp);
                        splist.Add(ctr.timkiem("","","","",0,0,null,loaisp_list,"1","id",true,0,-1));
                    }
                    else
                    {
                        foreach (NhomSanPham item in tmp.ds_nhomcon)
                        {
                            loaisp_list.Add(item);
                            splist.Add(ctr.timkiem_dequy(item, "1", 0, 3));
                        }
                    }
                }
                else
                {
                    tmp = null; loaisp = null;
                    List<NhomSanPham2> loaisp_l = ctr2.get_tree(tmp, 0);
                    foreach (NhomSanPham2 item in loaisp_l)
                    {
                       // if ((loaisp != null && loaisp.id != item.id && item.level == 1) || (loaisp == null && item.level == 0))
                        if(item.level==0&&item.active==true)
                        {
                            NhomSanPham a = ctr2.get_by_id(item.id);
                            loaisp_list.Add(a);
                            splist.Add(ctr.timkiem_dequy(a, "1", 0, 3));
                        }

                    }

                }
                ViewBag.splist = splist;
                ViewBag.loaisp = loaisp;
                ViewBag.loaisp_list = loaisp_list;
            }
            else
            {
                String orderby = TextLibrary.ToString(front_sanpham["front_orderby"]);
                Boolean desc = TextLibrary.ToBoolean(front_sanpham["front_desc"]);
                NhomSanPham tmp = ctr2.get_by_id(id_loaisp);
                loaisp.Load_From(tmp);
                loaisp.level = level_loaisp;
                foreach(var item in ctr2.get_tree2(tmp))
                {
                    if (item.active == true)
                        loaisp_list.Add(item);
                }
                //calculate offset
                if (TextLibrary.ToString(Request["front_current_page"]) != "") page = int.Parse(TextLibrary.ToString(Request["front_current_page"]));
                int max_item_per_page = TextLibrary.ToInt(this.front_sanpham["front_max_item_per_page"]);
                int start_point = (page - 1) * max_item_per_page;
                if (start_point <= 0) start_point = 0;
                //get list
                List<SanPham> listnew = ctr.timkiem("", "", "", "", -1, -1, null, loaisp_list, "1", orderby, desc, start_point, max_item_per_page);
                ViewBag.SanPham_List = listnew;
                ViewBag.front_sanpham = this.front_sanpham;
                ViewBag.loaisp = loaisp;
                //pagination
                int Current_Page = page;
                //   int Result_Count = listnew.Count;
                int Result_Count = ctr.timkiem_count("", "", "", "", -1, -1, null, loaisp_list, "1");
                int Total_Page = (int)(Math.Ceiling((double)Result_Count / max_item_per_page));
                Boolean CanNextPage = Current_Page >= Total_Page ? false : true;
                Boolean CanPrevPage = Current_Page == 1 ? false : true;
                ViewBag.Result_Count = Result_Count;
                ViewBag.CanNextPage = CanNextPage;
                ViewBag.CanPrevPage = CanPrevPage;
                ViewBag.Current_Page = Current_Page;
                ViewBag.Total_page = Total_Page;
            }
            return View();
        }
 private List<NhomSanPham2> _get_tree(NhomSanPham root, int level)
 {
     List<NhomSanPham> list;
     //lấy các child
     if (root == null)
     {
         list = this._db.ds_nhomsanpham.Where(x => x.nhomcha == null).ToList();
     }
     else
     {
          list = root.ds_nhomcon;
     }
     if (list == null) return new List<NhomSanPham2>();
     foreach (NhomSanPham item in list)
     {
         //add child đang xét vô cùng với level
         NhomSanPham2 tmp = new NhomSanPham2();
         tmp.Load_From(item);//load id, ten, ...
         tmp.level = level;
         this._tmp_for_get_tree.Add(tmp);
         //call recursive for this child
         this._get_tree(item, level+1);
     }
     //finish
     return this._tmp_for_get_tree;
 }