private void InitBody(int Id = 0, string CompanyIds = "") { if (!string.IsNullOrEmpty(CompanyIds)) { List <int> ids = CompanyIds.Split(',').Select(o => Convert.ToInt32(o)).ToList(); var CompanyList = CompanyRepo.GetMany(o => ids.Contains(o.Id)).ToList(); ViewBag.CompanyIdList = CompanyList; } else { ViewBag.CompanyIdList = new List <EM_Company>(); } var Dtos = userRoleRepo.GetPrograms(Id); var Vms = Mapper.Map <List <UserRoleProgramDTO>, List <UserRoleProgramVM> >(Dtos); var Systems = SystemType.YJ.GetEnumList().Where(o => o.Key != (int)SystemType.All).Select(o => new UserRoleTreeVM()//两个系统 { ParentId = 0, Id = o.Key, Name = o.Value, Items = new List <UserRoleTreeVM>() }).ToList(); foreach (var system in Systems)//遍历系统 { var Controls = Vms.Select(o => o.ControllerDescription).Distinct().Select(o => new UserRoleTreeVM() { Id = 0, Name = o, ParentId = system.Id, Items = new List <UserRoleTreeVM>() }).ToList(); for (int i = 0; i < Controls.Count(); i++)//遍历每个控制器 { var VirtualId = 999 + i; Controls[i].Id = VirtualId; var ItemList = Vms.Where(o => o.ControllerDescription == Controls[i].Name).Select(o => new UserRoleTreeVM() { Id = o.Id, PerMit = o.PerMit, ParentId = VirtualId, Name = o.ActionDescription }).ToList(); Controls[i].Items = ItemList;//最终节点。action } system.Items = Controls; } ViewBag.Systems = Systems; }