查询Nested Set Model table,这是SQL ......如何在LINQ中编写?
SELECT parent.name
FROM nested_category AS node, nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'FLASH'
ORDER BY parent.lft;
特别是,FROM部分......从未尝试在LINQ中做过类似的事情。
答案 0 :(得分:4)
也许是这样的:
var query = from node in nested_category
from parentNode in nested_category
where node.lft >= parentNode.lft && node.rgt <= parentNode.rgt
&& node.name == "FLASH"
orderby parent.left
select parent.name;
答案 1 :(得分:1)
如果您执行LEFT OUTER加入(即不使用 进入 和 DefaultIfEmpty() )然后将不输出顶级父节点(因为它的父节点是NULL)
我的SQL版本:
SELECT
B.ELEMENT_CODE ParentElement,
A.ELEMENT_CODE ChildElement
FROM NESTED_SET_STRUCTURE AS A
LEFT OUTER JOIN NESTED_SET_STRUCTURE AS B
ON B.PROCEDURE = 'TEST1' AND B.FROM_LEFT = (SELECT MAX(C.FROM_LEFT)
FROM NESTED_SET_STRUCTURE AS C
WHERE C.PROCEDURE = 'TEST1' AND A.FROM_LEFT > C.FROM_LEFT
AND A.FROM_LEFT < C.TO_RIGHT)
WHERE
A.PROCEDURE = 'TEST1';
翻译为:
var nestedSets1 =
from a in NESTED_SET_STRUCTUREs
join b in NESTED_SET_STRUCTUREs on
new { a.PROCEDURE, FROM_LEFT = ((from c in NESTED_SET_STRUCTUREs
where c.PROCEDURE.Equals("TEST1")
&& a.FROM_LEFT > c.FROM_LEFT
&& a.FROM_LEFT < c.TO_RIGHT
select (c.FROM_LEFT)).Max()) } equals
new { b.PROCEDURE, b.FROM_LEFT } into bo
from bb in bo.DefaultIfEmpty()
where
a.PROCEDURE.Equals("TEST1")
orderby a.FROM_LEFT
select new {
ParentElement = bb.ELEMENT_CODE,
ChildElement = a.ELEMENT_CODE
};
希望有所帮助,保罗。