生成系统目录
之前是先获取到整体的集合对象,然后使用递归的方式将信息进行整合。
新方式,学习一下
public List<ProjectMenuViewModel> ToTree(List<ProjectMenuViewModel> source)
{
if (source == null)
{
return new List<ProjectMenuViewModel>();
}
foreach (var node in source)
{
if (node.UpId > 0)
{
if (!source.Any(n => n.MenuId == node.UpId))
{
node.UpId = 0;
}
}
}
source = source.OrderBy(m => m.Order).ThenBy(m => m.Id).ToList();
var currentLevel = source.Where(q => q.UpId == 0).ToList();
while (true)
{
foreach (var temp in currentLevel)
{
temp.Children = source.Where(q => q.UpId == temp.MenuId).ToList();
}
currentLevel = currentLevel.SelectMany(q => q.Children).ToList();
if (currentLevel.Count == 0)
{
break;
}
}
return source.Where(q => q.UpId == 0).ToList();
}