c# - Representing a tree structure out of a db -


I have read about the various ways of representing a hierarchical structure within a relational database such as an attachment list.

I have decided to try a straight-forward like table (overseas): id | Name | Parents where there is an internal reference of parent id .

This undetermined depth should be enough to represent a simple tree.

Now, how can I create a tree to represent such data structure? As such, if I'm XML or Nested & lt; Ul & gt; & Lt; Li & gt; I want to print a series of in HTML format, what is the most effective way of loop through nodes? I wonder if Linq can come to help, but I am also more interested in normal (not .net bound) and theoretical answer.

Thanks in advance.

Ironically, the hardest part of this problem can be to obtain a limited set of records from a database that meets the understanding of a particular path. Unless you are using a database that provides hierarchical query support (such as by connecting the Oracle), it can be very complicated to look for some of the parts in that part.

But, suppose you managed to load your data ... in such a custom object collection, or in a dataset, or an XML structure (if you add data other than strings Do not care about breaking). If you have a custom object, you can add that property, which reveals the hair collection by asking the entire data set for the matching records on the original collection (see the example below). If the number of records is large, then it starts to become non-executable due to the cost of doing archive searches. This is where the dataset solution shines - as you can add a custom index to the ParentID column, which will improve the look of the lookup. How can you increase your example in this direction, you can go here to learn about it.

Here is an example of a custom object with tree navigation support. I am using a directory structure because it is a natural example of hierarchical data. Then, be careful not to use this code without an indexed structure, because a complete child-traversal will be o (n 2 ).

  class folder // can be changed to a Detroit-derived (Public Ent ID); Set; } Public string name {get; Set; } Public int? ParentId {Receive; Set; } Public IEnumerable & lt; Folder & gt; GetChildren (IEnumerable & lt; Folder & gt; allFolders) {// Note: Unindexed data / becomes expensive on a better hierarchy / slightly better version, IEnumerable & lt; Folder & gt; // with iOrderedEnumerable & lt; Folder & gt; Which will allow the use of a binary search algorithm to find children (assuming this value is on the parent property). Return AllFolders Where (x => x.ParentId.HasValue & x.ParentId.Value = this.Id); }} // Somewhere else in your code ... Zero LoadAndPrintFolders () {// Load folder data ... from the database, or wherever IEnumerable & lt; Folder & gt; AllFolders = LoadFolders (); // Find all root folders (we say, those who have a nulled parent id ienumerable & lt; folder & gt; root folders = all folders. Where (x => X.ParentId.HasValue); // Repeat data as a tree structure ... foreach (root folder in the root folder) {PrintFolder (all folders, folders, 0);}} // recursive function to print all folders ... zero printfolder ( IEnumerable & lt; folder & gt; All Folders, Folder F, Unt level {Console (printfolter (all folders, folder, level + 1) .itine ("{0} {1}", new string ('\ t', level), f.Name); foreach (in various folders F.GetChildren (allFolders);}}  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -