Ejemplo n.º 1
0
        public async Task <IEnumerable <WellModel> > Get()
        {
            var wells = (await _wellRepository.Get()).ToList();

            var companyIds  = wells.Select(x => x.CompanyId).Where(x => x != default).Distinct().ToList();
            var workshopIds = wells.Select(x => x.WorkshopId).Where(x => x != default).Distinct().ToList();
            var fieldIds    = wells.Select(x => x.FieldId).Where(x => x != default).Distinct().ToList();

            var companiesTask = Task.FromResult(Enumerable.Empty <CompanyModel>());

            if (companyIds.Any())
            {
                companiesTask = _companyRepository.GetByIds(companyIds);
            }

            var workshopsTask = Task.FromResult(Enumerable.Empty <WorkshopModel>());

            if (workshopIds.Any())
            {
                workshopsTask = _workshopRepository.GetByIds(workshopIds);
            }

            var fieldsTask = Task.FromResult(Enumerable.Empty <FieldModel>());

            if (fieldIds.Any())
            {
                fieldsTask = _fieldRepository.GetByIds(fieldIds);
            }

            await Task.WhenAll(companiesTask, workshopsTask, fieldsTask);

            foreach (var well in wells)
            {
                var company  = companiesTask.Result.FirstOrDefault(x => x.Id == well.CompanyId);
                var workshop = workshopsTask.Result.FirstOrDefault(x => x.Id == well.WorkshopId);
                var field    = fieldsTask.Result.FirstOrDefault(x => x.Id == well.FieldId);

                well.Company  = company;
                well.Workshop = workshop;
                well.Field    = field;
            }

            return(wells);
        }