简体   繁体   中英

IQueryable into a hierarchy

I currently have an IQueryable of Questions. In my Question object I have and "id" and a "parentId" which can be used to create a hierarchy. Currently, I bind a RadTreeView to the IQueryable of Questions and the RadTreeView takes care of creating the hierarchy because I define the dataId and dataParentId for the TreeView in the markup. I am now in need of actually having the data sorted into a hierarchy PRIOR to passing it off to my RadTreeView. How can I do this?

You could add a numeric field to the table, and populate it by recursively walking the tree and filling the numeric field with an incrementing value.

ParentID   ID      Sequence
1          7       1
1          4       2
1          9       3
2          6       4
2          5       5
2          3       6

..etc.

If you have access to the SQL Server, you can set something up using Common Table Expressions to return data in the proper order.. See:

http://a-kicker-n.blogspot.com/2009/04/how-to-sort-hierarchical-recursive.html

See also: Hierarchical data in Linq - options and performance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM