我已经四处寻找并花了很长时间尝试将此SQL语句转换为VB中的Linq语句。我相信这对其他人来说是一个很好的例子 - 该声明试图推出与产品类别具有多对多关系的产品,并且这些类别具有父母/子女的等级。
以下是我要转换的查询:
SELECT P.ProductID, P.ProductName, P.ProductSlug, P.PartNumber
FROM Products AS P
INNER JOIN Products_Categories AS PC ON PC.ProductID = P.ProductID
INNER JOIN Categories AS C ON PC.CategoryID = C.CategoryID
LEFT OUTER JOIN Categories AS P_Cats ON P_Cats.CategoryID = C.Parent
WHERE (C.CategoryID = 9) OR (C.Parent = 9) OR (P_Cats.Parent = 9)
我可以说到我试图说“哪里......(P_Cats.Parent = 9)”,但无法确定那部分。
谢谢!
答案 0 :(得分:1)
在发布问题后立即弄明白,但是如果其他人发现它有帮助,这里是答案:
Dim query = (From products In db.Products _
Join PC In db.Products_Categories On PC.ProductID Equals products.ProductID _
Join C In db.Categories On PC.CategoryID Equals C.CategoryID _
Group Join cat In db.Categories On cat.CategoryID Equals C.Parent Into C_Parents = Group _
From cParents In C_Parents.DefaultIfEmpty() _
Where (C.CategoryID = categoryID Or C.Parent = categoryID Or cParents.CategoryID = categoryID) _
And products.IsDeleted = False)
我要离开的是“来自cParents在C_Parents.DefaultIfEmpty()”的行。