码迷,mamicode.com
首页 > 其他好文 > 详细

递归调用

时间:2015-11-20 19:11:41      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

public List<GroupNodeModel> GetGroupTree(Guid? groupId)
{
List<GroupNodeModel> result = new List<GroupNodeModel>();
List<Group> groups;
using (Dal dal = new Dal())
{
groups = dal.Get<Group>().ToList();
}

GetGroupTree(groups, result, groupId, true);


return result;

}
private void GetGroupTree(List<Group> groups, List<GroupNodeModel> tree, Guid? groupId, bool isTop)
{
List<Group> childs;
if (isTop)
{
if (groupId.HasValue)
{
childs = groups.Where(r => r.ID == groupId).ToList();
}
else
{
childs = groups.Where(r => r.ParentId == null).ToList();
}
}
else
{
childs = groups.Where(r => r.ParentId == groupId).ToList();
}
foreach (Group group in childs)
{
GroupNodeModel node = new GroupNodeModel()
{
ID = group.ID,
GroupName = group.GroupName,
children = new List<GroupNodeModel>()
};
tree.Add(node);
GetGroupTree(groups, node.children, group.ID, false);
}

}

递归调用

标签:

原文地址:http://www.cnblogs.com/zhaoshuang/p/4981307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!