linq to xml/動態類型 從樹型表構建樹(linq to entity處理資料庫)

來源:互聯網
上載者:User

有疑問的可以去下面地址聯絡我 歡迎拍磚!

http://bbs.ctrlc.com.cn/forum.php?mod=viewthread&tid=594&extra=page%3D1


資料庫

linq to xml方法

public dynamic GetAllTree()        {            List<MenuClassify> roots = GetAllRoot();//取得所有根節點            //建立xml            XDocument tree = new XDocument();            tree.Declaration = new XDeclaration("1.0", "utf-8", "no");            tree.Add(new XElement("Tree"));               //添加樹            foreach (var menuClassify in roots)            {                XElement node=new XElement("Node");//添加節點                node.SetAttributeValue("name", menuClassify.Name);//為節點添加屬性                node.SetAttributeValue("id", menuClassify.MenuClassifyID);                tree.Element("Tree").Add(node);//將節點加入樹                NextNode(menuClassify.MenuClassifyID,node);//開始遞迴            }             string xmlstr = tree.ToString();            XmlDocument doc = new XmlDocument();            doc.LoadXml(xmlstr);            string jsonStr = JsonConvert.SerializeXmlNode(doc);            return tree;           }
 public void NextNode(int id,XElement parentNode)        {            var nodes = DBHelper.Entity.MenuClassifies.Where(m => m.ParentID == id).Select(m => m);//獲得次父節點的所有子節點            foreach (var node in nodes)            {                                              XElement xNode=new XElement("Node");                parentNode.Add(xNode);                xNode.SetAttributeValue("name",node.Name);                xNode.SetAttributeValue("id",node.MenuClassifyID);                NextNode(node.MenuClassifyID,xNode);//遞迴遍曆            }        }

dynamic方式構建

public dynamic GetAllTree()        {            List<MenuClassify> roots = GetAllRoot();//取得所有根節點            List<dynamic> tree=new List<dynamic>();            foreach (var root in roots)            {                dynamic node = new ExpandoObject();//建立節點                node.name = root.Name;//為屬性賦值                node.id = root.MenuClassifyID;                tree.Add(node);//將節點加入樹                NextNode(root.MenuClassifyID,node);//開始遞迴            }            string jsonstr = JsonConvert.SerializeObject(tree);//test部分 序列化成json            return tree;        }        public void NextNode(int id,dynamic parentNode)        {            var nodes = DBHelper.Entity.MenuClassifies.Where(m => m.ParentID == id).Select(m => m);            if (nodes.ToList().Count != 0)//存在子節點            {                List<dynamic> list = new List<dynamic>();                parentNode.nodes = list;                foreach (var node in nodes)                {                    dynamic n = new ExpandoObject();                    n.name = node.Name;                    n.id = node.MenuClassifyID;                    list.Add(n);                    NextNode(node.MenuClassifyID, n);                }            }        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.