标签:
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