public Guid AddOrUpdate(Drug d, Application app) { var drugKey = new ImporterCacheKeyDrug { Ingredient = d.Ingredient, DosageForm = d.DosageForm, DrugType = d.DrugType, Route = d.RouteOfAdmin }; Drug dMemory; Guid drugId; if (Drugs.TryGetValue(drugKey, out dMemory)) { dMemory.PharmaClassesText = dMemory.PharmaClassesText.Concat(d.PharmaClassesText).Distinct().ToList(); dMemory.PharmaClasses = dMemory.PharmaClasses.Concat( PharmaClasses.Where(x => d.PharmaClassesText.Any(y => y == x.Key)).Select(x => x.Value.Id).ToList() ).Distinct().ToList(); dMemory.Strengths = dMemory.Strengths.Concat(d.Strengths).Distinct().ToList(); dMemory.TradeNames = dMemory.TradeNames.Concat(d.TradeNames).Distinct().ToList(); if (dMemory.Schedule == null) { dMemory.Schedule = d.Schedule; } if (dMemory.StartMarketingDate != null && d.StartMarketingDate.HasValue && d.StartMarketingDate < dMemory.StartMarketingDate) { dMemory.StartMarketingDate = d.StartMarketingDate; } else { dMemory.StartMarketingDate = d.StartMarketingDate; } drugId = dMemory.Id; } else { d.Id = Guid.NewGuid(); drugId = d.Id; d.PharmaClasses = PharmaClasses.Where(x => d.PharmaClassesText.Any(y => y == x.Key)).Select(x => x.Value.Id).ToList(); Drugs.Add(drugKey, d); } if (app != null) { app.DrugId = drugId; ImporterCacheKeyApplication appKey = new ImporterCacheKeyApplication { AppNo = app.ApplicationNumber, ProductNo = app.ProductNumber }; if (!AppToDrug.ContainsKey(appKey)) { app.Id = Guid.NewGuid(); AppToDrug.Add(appKey, drugId); AppToApp.Add(appKey, app.Id); Apps.Add(app); } } return(drugId); }