从Linq获取嵌套集模型树的子树

时间:2017-02-22 14:09:51

标签: linq tsql tree nested

我已经发现这个T-SQL查询(并且它可以)从指定节点返回整个子树:

SELECT node.id
FROM cuenta AS node,
        cuenta AS parent,
        cuenta AS sub_parent,
        (
                SELECT node.id, (COUNT(parent.id) - 1) AS depth
                FROM cuenta AS node,
                cuenta AS parent
                WHERE node.lft BETWEEN parent.lft AND parent.rgt
                AND node.id = 16
                GROUP BY node.id
        )AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
        AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
        AND sub_parent.id = sub_tree.id
GROUP BY node.id

如何在c#linq查询中对此进行编码?

我已经尝试过这个,但它不起作用:

                var nodeList = (from node in db.GetAll<Cuenta>()
                                from parent in db.GetAll<Cuenta>()
                                from sub_parent in db.GetAll<Cuenta>()
                                from sub_tree in (
                                    from node2 in db.GetAll<Cuenta>()
                                    from parent2 in db.GetAll<Cuenta>()
                                    where node2.Lft >= parent2.Lft && node2.Lft <= parent2.Rgt && node2.Id == nodeId
                                    group node2 by node2.Id into g
                                    //orderby node2.Lft
                                    select new { Id = g.Key, Depth = g.Count() - 1 })
                                where
                                    node.Lft >= parent.Lft && node.Lft <= parent.Rgt &&
                                    node.Lft >= sub_parent.Lft && node.Lft <= sub_parent.Rgt &&
                                    sub_parent.Id == sub_tree.Id
                                group node by node.Id into g2
                                //orderby node.Lft
                                //select new { Id = g.Key, Depth = g2.Count() - sub_tree.Depth + 1 });
                                select new { Id = g2.Key }).ToList();

谢谢!

1 个答案:

答案 0 :(得分:0)

我解决了它:

            var nodeList = (from node in db.GetTable<Cuenta>()
                            from parent in db.GetTable<Cuenta>()
                            from sub_parent in db.GetTable<Cuenta>()
                            from sub_tree in (
                                from node in db.GetTable<Cuenta>()
                                from parent in db.GetTable<Cuenta>()
                                where node.Lft >= parent.Lft && node.Lft <= parent.Rgt && node.Id == nodeId
                                group node by node.Id into g
                                select new { Id = g.Key, Depth = g.Count() - 1 })
                            where
                                node.Lft >= parent.Lft && node.Lft <= parent.Rgt &&
                                node.Lft >= sub_parent.Lft && node.Lft <= sub_parent.Rgt &&
                                sub_parent.Id == sub_tree.Id
                            group node by node.Id into g
                            select new { Id = g.Key }).ToList();