0

I have a parent child table of Evolution progress in my db

 EvolutionID   ParentID  otherFields
 A0              NULL      *
 A1              A0        *
 A2              A1        *
 B0              NULL      *
 B1              B0        *
 B2              B1        *

I need to display in my asp.net-mvc page like this:

 -------------------------------------
 |  A0       |  A1       |  A2       |
 |  field.*  |  fields.* |  fields.* | 
 -------------------------------------
 |  B0       |  B1       |  B2       |
 |  field.*  |  fields.* |  fields.* | 
 -------------------------------------

What I can do now is first read the Evolutions with parentID = null

 A0
 B0

and then create a list for each one using a function to bring the childs from db:

  List<Evolution> A0 = GetEvolutionListFor ('A0');
  List<Evolution> B0 = GetEvolutionListFor ('B0');

Finally pass the List in a ViewBag

  List<List<Evolution>> EvolutionList = New List<List<Evolution>>() {
        A0, A1 };
  ViewBag.Evolutions = EvolutionList ;

I dont have problem with the display, I can create a Evolution template to render the object in each table cell.

My problem is what is the best way to bring the data to the model.

Right now is one query to bring the root nodes and one more query for each node to bring the list.

Should I retrive all the rows at once and build the lists on my client app. Or is there a way I can retrive that data direct from database?

Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • Not directly related, but refer [this answer](https://stackoverflow.com/questions/46560515/how-to-create-dynamic-menu-using-tree/46562343#46562343) for using a single query with `.ToLookup()` to build a hierarchical model –  Apr 07 '18 at 20:56
  • Thanks @StephenMuecke sound good, I can bring the whole data in a single read and build the lists. – Juan Carlos Oropeza Apr 08 '18 at 00:16

1 Answers1

0

Assuming you can keep all of the records in memory at the same time, you should do one query of all the objects and then build the data structure in memory. Assuming you are using Entity Framework and have a Parent relation it would be something along the lines of:

List<Evolutions> evolutions; 
using (var context = new MyContext()) {
    evolutions = context.Evolutions.Include( e => e.Parent);
}

Then you would build your data structure in memory.

sjb-sjb
  • 1,112
  • 6
  • 14