2013-05-30 339 views
5

我有一個SQL查詢:在轉換的SQL查詢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; 

我試圖將其轉換爲LINQ自己,但我不熟悉的語言,經過一番研究,我已經試過使用Linqer但它不會轉換函數'BETWEEN'或'COUNT'。

到目前爲止,我已經得到的最接近的是:

 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; 

不工作,也不會返回「深度」,即使它沒有,請幫助!

編輯:

該查詢用於返回節點的深度在一組嵌套的順序,這樣我可以創建層次結構的表示;我下面這個教程:在章http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/「尋找深度的節點」

+0

你應該使用'> ='和'<='在LINQ聯接,因爲'BETWEEN'是包容 –

+0

注意,但我會考慮你的表格結構 – LukeHennerley

+1

爲什麼你需要知道前面的深度?難道你不能返回一個嵌套的結構,並讓繪製界面的代碼保持深度計數器? – MrFox

回答

6

這應該讓你關閉。

鑑於

 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 }, 
     }; 

那麼這

 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 }; 

產生

{ 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

很好,謝謝,返回除了根節點之外的所有東西,但是按照字母順序排列,而不是按照「LeftID」的順序排列。此外,它應該是「在db.CompanyGroups」 – Jimmy

+0

@JimBarton如果你改變哪裏的第一位是哪裏是哪裏node.LeftID> parent.LeftID'那麼你應該得到根節點 –

+0

我已經修改了答案添加排序和根節點。如果需要,您可以將companyGroup替換爲db.CompanyGroups。 – scdove

0

必須添加類似:

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 } 
     } 
};