public IDataEntity BuildLearningDeliveryDataEntity(ILearningDelivery learningDelivery)
        {
            var learningDeliveryFAMDenormalized = BuildLearningDeliveryFAMDenormalized(learningDelivery.LearningDeliveryFAMs);
            var larsLearningDelivery            = _larsReferenceDataService.LARSLearningDeliveryForLearnAimRef(learningDelivery.LearnAimRef);
            var larsFrameworkAims = _larsReferenceDataService.LARSFFrameworkAimsForLearnAimRef(learningDelivery.LearnAimRef);
            var larsFunding       = _larsReferenceDataService.LARSFundingsForLearnAimRef(learningDelivery.LearnAimRef);

            var larsAnnualValue = _larsReferenceDataService.LARSAnnualValuesForLearnAimRef(learningDelivery.LearnAimRef);
            var larsLearningDeliveryCategories = _larsReferenceDataService.LARSLearningDeliveryCategoriesForLearnAimRef(learningDelivery.LearnAimRef);
            var sfaAreaCost = _postcodesReferenceDataService.SFAAreaCostsForPostcode(learningDelivery.DelLocPostCode);

            var larsFwkAims = larsFrameworkAims?.ToList();

            int?frameworkComponentType = null;

            if (larsFrameworkAims != null &&
                learningDelivery.FworkCodeNullable != null &&
                learningDelivery.ProgTypeNullable != null &&
                learningDelivery.PwayCodeNullable != null)
            {
                frameworkComponentType = larsFrameworkAims
                                         .Where(fwa =>
                                                learningDelivery.FworkCodeNullable == fwa.FworkCode &&
                                                learningDelivery.ProgTypeNullable == fwa.ProgType &&
                                                learningDelivery.PwayCodeNullable == fwa.PwayCode)
                                         .Select(fwct => fwct.FrameworkComponentType).FirstOrDefault();
            }

            return(new DataEntity(Attributes.EntityLearningDelivery)
            {
                Attributes = new Dictionary <string, IAttributeData>()
                {
                    { Attributes.AchDate, new AttributeData(learningDelivery.AchDateNullable) },
                    { Attributes.AddHours, new AttributeData(learningDelivery.AddHoursNullable) },
                    { Attributes.AimSeqNumber, new AttributeData(learningDelivery.AimSeqNumber) },
                    { Attributes.AimType, new AttributeData(learningDelivery.AimType) },
                    { Attributes.CompStatus, new AttributeData(learningDelivery.CompStatus) },
                    { Attributes.EmpOutcome, new AttributeData(learningDelivery.EmpOutcomeNullable) },
                    { Attributes.EnglandFEHEStatus, new AttributeData(larsLearningDelivery.EnglandFEHEStatus) },
                    { Attributes.EnglPrscID, new AttributeData(larsLearningDelivery.EnglPrscID) },
                    { Attributes.FworkCode, new AttributeData(learningDelivery.FworkCodeNullable) },
                    { Attributes.FrameworkCommonComponent, new AttributeData(larsLearningDelivery.FrameworkCommonComponent) },
                    { Attributes.FrameworkComponentType, new AttributeData(frameworkComponentType) },
                    { Attributes.LearnActEndDate, new AttributeData(learningDelivery.LearnActEndDateNullable) },
                    { Attributes.LearnPlanEndDate, new AttributeData(learningDelivery.LearnPlanEndDate) },
                    { Attributes.LearnStartDate, new AttributeData(learningDelivery.LearnStartDate) },
                    { Attributes.LrnDelFAM_EEF, new AttributeData(learningDeliveryFAMDenormalized.EEF) },
                    { Attributes.LrnDelFAM_LDM1, new AttributeData(learningDeliveryFAMDenormalized.LDM1) },
                    { Attributes.LrnDelFAM_LDM2, new AttributeData(learningDeliveryFAMDenormalized.LDM2) },
                    { Attributes.LrnDelFAM_LDM3, new AttributeData(learningDeliveryFAMDenormalized.LDM3) },
                    { Attributes.LrnDelFAM_LDM4, new AttributeData(learningDeliveryFAMDenormalized.LDM4) },
                    { Attributes.LrnDelFAM_FFI, new AttributeData(learningDeliveryFAMDenormalized.FFI) },
                    { Attributes.LrnDelFAM_RES, new AttributeData(learningDeliveryFAMDenormalized.RES) },
                    { Attributes.OrigLearnStartDate, new AttributeData(learningDelivery.OrigLearnStartDateNullable) },
                    { Attributes.OtherFundAdj, new AttributeData(learningDelivery.OtherFundAdjNullable) },
                    { Attributes.Outcome, new AttributeData(learningDelivery.OutcomeNullable) },
                    { Attributes.PriorLearnFundAdj, new AttributeData(learningDelivery.PriorLearnFundAdjNullable) },
                    { Attributes.ProgType, new AttributeData(learningDelivery.ProgTypeNullable) },
                    { Attributes.PwayCode, new AttributeData(learningDelivery.PwayCodeNullable) }
                },
                Children = (
                    learningDelivery?
                    .LearningDeliveryFAMs?
                    .Select(BuildLearningDeliveryFAM) ?? new List <IDataEntity>())
                           .Union(
                    larsAnnualValue?
                    .Select(BuildLARSAnnualValue) ?? new List <IDataEntity>())
                           .Union(
                    larsLearningDeliveryCategories?
                    .Select(BuildLARSLearningDeliveryCategories) ?? new List <IDataEntity>())
                           .Union(
                    sfaAreaCost?
                    .Select(BuildSFAAreaCost) ?? new List <IDataEntity>())
                           .Union(
                    larsFunding?
                    .Select(BuildLARSFunding) ?? new List <IDataEntity>())
                           .ToList()
            });
        }