Csharp: DataRelation objects to represent a parent/child/Level relationship, csharpdatarelation

Source: Internet
Author: User

Csharp: DataRelation objects to represent a parent/child/Level relationship, csharpdatarelation

/// <Summary> //// </summary> /// <param name = "sender"> </param> /// <param name = "e "> </param> protected void Page_Load (object sender, eventArgs e) {var sections = new List <Section> {new Section {Id = 1, Name = "China", ParentID = 0}, new Section {Id = 2, name = "Jiangxi", ParentID = 1}, new Section {Id = 3, Name = "Jiangsu", ParentID = 1}, new Section {Id = 4, name = "Nanjing", ParentID = 3}, new Section {Id = 5, Name = "Nanchang", ParentID = 2}, new Section {Id = 6, Name = "Donghu district", ParentID = 5 }, new Section {Id = 7, Name = "Guangdong", ParentID = 1}, new Section {Id = 8, Name = "Shenzhen", ParentID = 7 }, new Section {Id = 9, Name = "Luohu tu juwen", ParentID = 8 }}; // sections = sections. orderBy (x => x. parentID ). thenBy (x => x. name ). toList (); // var stack = new Stack <Section> (); // Grab all the items without pare NT // foreach (var section in sections. where (x => x. parentID = default (int )). reverse () // {// stack. push (section); // sections. removeAt (0); //} var output = new List <Section> (); // while (stack. any () // {// var currentSection = stack. pop (); // var children = sections. where (x => x. parentID = currentSection. id ). reverse (); // foreach (var section in children) // {// stack. push (section); // sectio Ns. remove (section); //} // output. add (currentSection); // sections = output; List <MySection> mys = MenuHelper. getMyMenuCollection (sections); // ResolveDDL <MySectionMenu> (mys); var outputlist = (from mysection in mys orderby mysection. treeLevel descending select mysection ). toList (); var outputstringlist = (from mysection in mys orderby mysection. treeLevel descending select mysection. name ). toList (); For (int I = 0; I <outputlist. count; I ++) {// Response. write (string. format ("ID: {0} ParentID: {1} TreeLevel: {2} Name: {3} <br/>", mys [I]. id, mys [I]. parentID, mys [I]. treeLevel, mys [I]. name); Response. write (string. format ("ID: {0} ParentID: {1} TreeLevel: {2} Name: {3} <br/>", outputlist [I]. id, outputlist [I]. parentID, outputlist [I]. treeLevel, outputlist [I]. name) ;}//< summary> //// </summary> /// <Typeparam name = "T"> </typeparam> // <param name = "mys"> </param> protected void ResolveDDL <T> (List <T> mys) where T: MyBaseSection, new () {ResolveDDL <T> (mys,-1, true );} /// <summary> ///// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "mys "> </param> // <param name =" currentId "> </param> protected void ResolveDDL <T> (List <T> mys, int currentId) where T: MyBaseSection, new () {ResolveDDL <T> (mys, currentId, true );} /// <summary> /// put a tree structure in the following list for selection /// </summary> /// <typeparam name = "T"> </ typeparam> // <param name = "currentId"> </param> // <param name = "mys"> </param> protected void ResolveDDL <T> (List <T> mys, int currentId, bool addRootNode) where T: MyBaseSection, new () {if (addRootNode) {// Add TreeLevel 1 for all nodes, and then add the root node foreach (T my in mys) {my. treeLevel + = 1;} T root = new T (); root. name = "-- root node --"; root. id = 0; root. treeLevel = 0; mys. insert (0, root);} // currentId =-1 indicates that the current node does not exist if (currentId! =-1) {// This node cannot be clicked (that is, the current node cannot be the parent node of the current node) // and all the child nodes of this node cannot be clicked, if you want to run the current node to the child node, the child nodes will disappear from the tree bool startChileNode = false; int startTreeLevel = 0; foreach (T my in mys) {if (my. id = currentId) {startTreeLevel = my. treeLevel; my. enabled = false; startChileNode = true;} else {if (startChileNode) {if (my. treeLevel> startTreeLevel) {my. enabled = false;} else {startChileNode = false ;}}}}}}} /// <summary> //// </summary> public class Section {public int Id {get; set;} public string Name {get; set ;} public int ParentID {get; set ;}/// <summary >///// </summary> public class MySection {public int Id {get; set ;} public string Name {get; set;} public int ParentID {get; set;} public int TreeLevel {get; set ;}} /// <summary> ///// </summary> public class MySectionMenu: myBaseSection {} // <summary> //// </summary> public class MyBaseSection {public int Id {get; set;} public int ParentId {get; set;} public string Name {get; set;} /// <summary> // The menu is in the middle of the tree structure (starting from 0) /// </summary> public int TreeLevel {get; set ;}/// <summary> // whether it is available (true by default) /// </summary> public bool Enabled {get; set ;}/// <summary> // whether the node is a leaf node (false by default) /// </summary> public bool IsTreeLeaf {get; set ;}} /// <summary> ///// </summary> public class MenuHelper {/// <summary> //// </summary> // <param name = "oldMenus"> </param> // <returns> </returns> public static List <MySection> GetMyMenuCollection (List <Section> oldMenus) {List <MySection> newMenus = new List <MySection> (); ResolveMenuCollection (oldMenus, newMenus, 0, 0); return newMenus ;} /// <summary> ///// </summary> /// <param name = "oldMenus"> </param> /// <param name = "newMenus "> </param> /// <param name =" parentId "> </param> // <param name =" level "> </param> // <returns> </returns> private static int ResolveMenuCollection (List <Section> oldMenus, list <MySection> newMenus, int parentId, int level) {int count = 0; foreach (Section menu in oldMenus) {if (menu. parentID = parentId) {count ++; MySection my = new MySection (); newMenus. add (my); my. treeLevel = level; my. id = menu. id; my. name = menu. name; my. parentID = menu. parentID; level ++; int childCount = ResolveMenuCollection (oldMenus, newMenus, menu. id, level); level -- ;}} return count ;}}

  

 

Display result:

ID: 1 ParentID: 0 TreeLevel: 0 Name: China
ID: 2 ParentID: 1 TreeLevel: 1 Name: Jiangxi
ID: 5 ParentID: 2 TreeLevel: 2 Name: Nanchang
ID: 6 ParentID: 5 TreeLevel: 3 Name: Donghu District
ID: 3 ParentID: 1 TreeLevel: 1 Name: Jiangsu
ID: 4 ParentID: 3 TreeLevel: 2 Name: Nanjing
ID: 7 ParentID: 1 TreeLevel: 1 Name: Guangdong
ID: 8 ParentID: 7 TreeLevel: 2 Name: Shenzhen
ID: 9 ParentID: 8 TreeLevel: 3 Name: Tu juwen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.