Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - How to convert from flat to hierarchical?

Tags:

linq

I have the following result set that was generated as the result of a Linq query. I would like to convert this to a hierarchical result set. The first two columns would represent the 'master' row, columns 3 and 4 would represent a child list of the master row, and column 5 and 6 would represent a second child list of the master row. The columns containing the value 1971 are the joining columns.

The end result should be a master object with a list of containers (G2 columns) and a list of printers (G3 columns).

What would the query look like to convert this to hierarchical form?

G1_ID   G1_CellName  G2_ContainerID G2_ID   G2_SerialNumber G3_ID      G3_PrinterName
1971    Default Cell    1935           1971    1101929         1971       PBG-PrtEmulator1
1971    Default Cell    1936           1971    1101930         1971       PBG-PrtEmulator1
1971    Default Cell    2189           1971    1102183         1971       PBG-PrtEmulator1
like image 886
Randy Minder Avatar asked Nov 29 '10 13:11

Randy Minder


1 Answers

groupby?

var result = from eachData in data
group eachData by new{ eachData .G1_ID, eachData .G1_CellName }
into g1
from eachG1 in g1
group eachG1 by new { eachG1.G2_..., eachG1.G2_... }
into g2
for eachG2 in g2
group eachG2 by new { eachG2.G3_... }
into g3
select g3;

haven't test it. but i'm sure its gonna look just like this.

like image 62
Bonshington Avatar answered Sep 30 '22 20:09

Bonshington