private List <Company> GetCompanies(SecurityProvider provider, List <CTDMUSRCIA> users) { var result = new List <Company>(); foreach (CTDMUSRCIA user in users) { if (result.ExistsCompanyId(user.CTUCCODCIA)) { continue; } var companyId = user.CTUCCODCIA; var companyDescription = provider.CompanyDescription(companyId); var branches = new List <Branch> ( ); foreach (CTDMUSRCIA USER in users.Where(usr => usr.CTUCCODCIA == companyId)) { var branchId = USER.CTUCCODSUC; var branchDescription = provider.BranchDescription(companyId, branchId); branches.Add(new Branch { Id = branchId, Description = branchDescription }); } var company = new Company { Id = companyId, Description = companyDescription, Branches = branches.ToArray( ) }; result.Add(company); } return(result); }