Display data hierarchically using LINQ

I have a department table with the following columns.

 Id (PK) Name ParentDepartmentId (FK of same Department table) 

Now I want to display all the data in a hierarchical order using LINQ. something like that:

  IT .Net Web Developer Windows Developer Java PHP Technical Support OffLine Online 

If IT has 3 sub-blocks .Net, Java, PHP. again .net has its own web developer and window developer etc.

How can I achieve this using LINQ. I am using ASP.Net MVC 3.0 using C #, Entity Framework, etc.

+4
source share
4 answers
+1
source

You need to use the GroupBy function.

0
source

Please view this link. It uses the .AsHierarchy() extension method to represent hierarchical data

http://www.scip.be/index.php?Page=ArticlesNET18#AsHierarchy .

The returned object will be in the tree structure. You just need to decide on your presentation logic (space for each node node)

0
source

You can create a simple set of Html in your action code to assign specific values ​​to your HTML that you dynamically create in your action with the values ​​you get with linq. Then when you browse, when you return it using viewdata, you present it as pure html and use jquery to create a tree here is a good example of a jquery tree plugin

http://mbraak.github.com/jqTree/

0
source

Source: https://habr.com/ru/post/1380332/


All Articles