public CompanyPaginatedDTO GetCompanies(int page, int limit) { List <Company> companies = new List <Company>(); CompanyPaginatedDTO companyPDTO = new CompanyPaginatedDTO(); int total; try { _logger.LogInformation($"{DateTime.Now:hh:mm:ss} Getting companies"); companies = _repository.GetCompanies(page, limit); total = _repository.CountComp(); } catch (Exception e) { _logger.LogInformation($"{DateTime.Now:hh:mm:ss} Error on getting companies"); throw new Exception($"Error is: {e.GetType()}"); } if (companies.Count() < 1) { throw new NotFoundException("Não existem empresas cadastradas"); } List <CompanyRegister> companyRegisters = new List <CompanyRegister>(); foreach (Company c in companies) { CompanyRegister company = new CompanyRegister(); company.CNPJ = c.CNPJ; company.CorporateSocialName = c.CorporateSocialName; company.FantasyName = c.FantasyName; company.Status = c.Status; List <UserCompany> usersCompanies = (List <UserCompany>)c.UserCompanies; foreach (UserCompany u in usersCompanies) { u.User = _userRepository.GetUserById(u.UserId); } company.CPF = usersCompanies.First(x => x.User.UserRole == Enums.UserRole.MANAGER).User.CPF; companyRegisters.Add(company); } companyPDTO.companyRegisters = companyRegisters; companyPDTO.total = total; return(companyPDTO); }