簡體   English   中英

將SQL查詢轉換為LINQ C#

[英]Converting an SQL query in to LINQ C#

我有一個SQL查詢:

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/關於“查找節點深度”一章

這應該讓你接近。

特定

        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 }

你必須添加如下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM