2013-05-30 14 views
5

Ho una query SQL:Conversione di una query SQL per LINQ C#

SELECT 
     node.GroupName 
    , depth = COUNT(parent.GroupName) - 1 
FROM CompanyGroup node 
JOIN CompanyGroup parent ON node.LeftID BETWEEN parent.LeftID AND parent.RightID 
GROUP BY node.GroupName, node.LeftID 
ORDER BY node.LeftID; 

Ho cercato di convertirlo in LINQ me stesso, ma io sono familiarità con la lingua, dopo alcune ricerche ho provato usando Linqer ma non converte le funzioni 'BETWEEN' o 'COUNT'.

Il più vicino che ho ottenuto finora è:

 var groupModel = 
      from node in db.CompanyGroups 
      join parent in db.CompanyGroups.Where(node.LeftID > parent.LeftID && node.LeftID < parent.RightID) 
      orderby node.LeftID 
      select node.GroupName; 

che non funziona e non sarebbe tornato la 'profondità' anche se lo facesse, aiuto per favore!

Edit:

La query viene utilizzata per restituire la profondità di nodi in un insieme nidificato in un ordine in modo che possa creare una rappresentazione di una gerarchia; Sto seguendo questo tutorial: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ sul capitolo 'Trovare la profondità dei nodi'

+0

si dovrebbe usare '> =' e '<=' nel LINQ join perché 'BETWEEN' è inclusiva –

+0

Nota a margine, ma vorrei prendere in considerazione la struttura della tabella – LukeHennerley

+1

Perché dovresti conoscere la profondità in anticipo? Non puoi restituire una struttura nidificata e lasciare che il codice che disegna l'interfaccia mantenga un contatore di profondità? – MrFox

risposta

6

Questo dovrebbe ottenere si chiude.

Dato

 var companyGroups = new List<CompanyGroup> 
     { 
      new CompanyGroup {GroupName = "ELECTRONICS",   LeftID = 1 , RightID =20 }, 
      new CompanyGroup {GroupName = "TELEVISIONS",   LeftID = 2 , RightID =9 }, 
      new CompanyGroup {GroupName = "TUBE",     LeftID = 3 , RightID =4 }, 
      new CompanyGroup {GroupName = "LCD",     LeftID = 5 , RightID =6 }, 
      new CompanyGroup {GroupName = "PLASMA    ", LeftID = 7 , RightID =8 }, 
      new CompanyGroup {GroupName = "PORTABLE ELECTRONICS", LeftID =10 , RightID =19 }, 
      new CompanyGroup {GroupName = "MP3 PLAYERS   ", LeftID =11 , RightID =14 }, 
      new CompanyGroup {GroupName = "FLASH    ", LeftID =12 , RightID =13 }, 
      new CompanyGroup {GroupName = "CD PLAYERS   ", LeftID =15 , RightID =16 }, 
      new CompanyGroup {GroupName = "2 WAY RADIOS  ", LeftID =17 , RightID =18 }, 
     }; 

allora questo

 var results = from node in companyGroups 
         from parent in companyGroups 
         where node.LeftID >= parent.LeftID && node.RightID <= parent.RightID 
         group node by node.GroupName into g 
         orderby g.First().LeftID 
         select new { GroupName = g.Key, Depth = g.Count() - 1 }; 

cede

{ GroupName = ELECTRONICS, Depth = 0 } 

{ GroupName = TELEVISIONS, Depth = 1 } 

{ GroupName = TUBE, Depth = 2 } 

{ GroupName = LCD, Depth = 2 } 

{ GroupName = PLASMA    , Depth = 2 } 

{ GroupName = PORTABLE ELECTRONICS, Depth = 1 } 

{ GroupName = MP3 PLAYERS   , Depth = 2 } 

{ GroupName = FLASH    , Depth = 3 } 

{ GroupName = CD PLAYERS   , Depth = 2 } 

{ GroupName = 2 WAY RADIOS  , Depth = 2 } 
+0

nice, thanks, restituisce tutto tranne il nodo radice con la profondità corretta ma in ordine alfabetico piuttosto che in ordine di 'LeftID'. Inoltre dovrebbe essere "in db.CompanyGroups" – Jimmy

+0

@JimBarton se si modifica il primo bit di dove essere 'where node.LeftID> = parent.LeftID' allora si dovrebbe ottenere il nodo root –

+0

Ho modificato la risposta per aggiungere ordinamento e il nodo radice. Se necessario, puoi scambiare companyGroup per db.CompanyGroups. – scdove

0

è necessario aggiungere qualcosa come:

group CompanyGroup by node.GroupName into g 
select new 
{ 
    node= g.GroupName, 
    left = select 
     new 
     { 

      left = 
      from CompanyGroup in g 
      group CompanyGroup by CompanyGroup. into mg 
      select new { left=mg.LeftID } 
     } 
}; 
Problemi correlati