如何计算C#中特定parent下的总child个节点?
How to count total child nodes under the specific parent in C#?
在过去的 2 天里,我一直在尝试计算 C# 中特定 parent 下的 child 节点。基本上我的数据库中有一个 SQL table,其中有 2 列:user_id、Users_parentId。示例:
__________________________
User_Id | Users_parentId
__________________________
100 | Noparent(main)
--------------------------
101 | 100(first User)
--------------------------
102 | 100
--------------------------
103 | 100
--------------------------
104 | 102 (3rd User)
--------------------------
105 | 100
--------------------------
106 | 102
--------------------------
107 | 102
--------------------------
111 | 107 (8th user)
--------------------------
112 | 107
--------------------------
115 | 105 (6th user)
--------------------------
222 | 105
--------------------------
225 | 112
--------------------------
336 | 112
--------------------------
666 | 112
如果我们从上面table生成一棵树,那么它会是这样的:
100
----------^-------------
| | | |
101 102 103 105
--------^------ ----^--------
| | | | |
104 106 107 115 222
------^-----
| |
111 112
------^------
| | |
225 336 666
所以在我的项目中我想计算所有child都在100
之下
基本上,我尝试使用 get child 列表,然后计算他们的 child,如果他们有,然后再次获取 grand_child 的 child 列表,等等,递归。
我试过使用 for 循环和 foreach 循环,但没有找到解决方案。
我想要在页面加载事件中计数总计 child(意味着现在 100 包含 14 child)。
当用户登录时,我想计算他下面的所有child。
我正在使用 Entity framework 和 LINQ 访问数据库,我的数据库名称是 GetUnitedDB,表名是 Office_Detail
如果上面提供的信息有任何错误或不完整,请告诉我。请在 C# 中建议逻辑。
您可以使用以下模板向 SQL 数据库添加视图:
;WITH UserTree AS
(
SELECT tn.User_Id UserId, tn.Users_parentId UserParentId, 0 AreaLevel
FROM Office_Detail tn
WHERE tn.Users_parentId = 100
UNION ALL
SELECT tn.User_Id, tn.Users_parentId, at.AreaLevel+1 AreaLevel
FROM UserTree at
INNER JOIN Office_Detail tn on at.UserId = cn.Users_parentId
)
select COUNT(UserId)
from UserTree
还可以考虑将 100 值更改为您用于 user_id 的类型的 参数,并将其发送到视图的请求中。
(此模板也可用于创建具有层次的树)
带递归的 C# 实现:
private static int Count(int OriginalId)
{
using (var ctx = new YourDBContext())
{
return FindAllSons(OriginalId, ctx);
}
}
private static int FindAllSons(int id, YourDBContext ctx)
{
var res = 1;
var children = ctx.TableName.Where(x => x.ParentId == id).Select(n => n.Id);
foreach(var child in children)
{
res += FindAllSons(child, ctx);
}
return res;
}
在过去的 2 天里,我一直在尝试计算 C# 中特定 parent 下的 child 节点。基本上我的数据库中有一个 SQL table,其中有 2 列:user_id、Users_parentId。示例:
__________________________
User_Id | Users_parentId
__________________________
100 | Noparent(main)
--------------------------
101 | 100(first User)
--------------------------
102 | 100
--------------------------
103 | 100
--------------------------
104 | 102 (3rd User)
--------------------------
105 | 100
--------------------------
106 | 102
--------------------------
107 | 102
--------------------------
111 | 107 (8th user)
--------------------------
112 | 107
--------------------------
115 | 105 (6th user)
--------------------------
222 | 105
--------------------------
225 | 112
--------------------------
336 | 112
--------------------------
666 | 112
如果我们从上面table生成一棵树,那么它会是这样的:
100 ----------^------------- | | | | 101 102 103 105 --------^------ ----^-------- | | | | | 104 106 107 115 222 ------^----- | | 111 112 ------^------ | | | 225 336 666
所以在我的项目中我想计算所有child都在100
之下
基本上,我尝试使用 get child 列表,然后计算他们的 child,如果他们有,然后再次获取 grand_child 的 child 列表,等等,递归。
我试过使用 for 循环和 foreach 循环,但没有找到解决方案。
我想要在页面加载事件中计数总计 child(意味着现在 100 包含 14 child)。
当用户登录时,我想计算他下面的所有child。
我正在使用 Entity framework 和 LINQ 访问数据库,我的数据库名称是 GetUnitedDB,表名是 Office_Detail
如果上面提供的信息有任何错误或不完整,请告诉我。请在 C# 中建议逻辑。
您可以使用以下模板向 SQL 数据库添加视图:
;WITH UserTree AS
(
SELECT tn.User_Id UserId, tn.Users_parentId UserParentId, 0 AreaLevel
FROM Office_Detail tn
WHERE tn.Users_parentId = 100
UNION ALL
SELECT tn.User_Id, tn.Users_parentId, at.AreaLevel+1 AreaLevel
FROM UserTree at
INNER JOIN Office_Detail tn on at.UserId = cn.Users_parentId
)
select COUNT(UserId)
from UserTree
还可以考虑将 100 值更改为您用于 user_id 的类型的 参数,并将其发送到视图的请求中。
(此模板也可用于创建具有层次的树)
带递归的 C# 实现:
private static int Count(int OriginalId)
{
using (var ctx = new YourDBContext())
{
return FindAllSons(OriginalId, ctx);
}
}
private static int FindAllSons(int id, YourDBContext ctx)
{
var res = 1;
var children = ctx.TableName.Where(x => x.ParentId == id).Select(n => n.Id);
foreach(var child in children)
{
res += FindAllSons(child, ctx);
}
return res;
}