public void MapColumns() { foreach (var header in Headers) { if (NutrientRegex.IsMatch(header)) { string nutrientId = NutrientRegex.Match(header).Groups["NutrientId"].Value; int columnIndex = Headers.IndexOf(header); NutrientMappings.Add(nutrientId, columnIndex); } if (NutrientRecRegex.IsMatch(header)) { string columnName = NutrientRecRegex.Match(header).Groups["NutrientId"].Value; string recNum = NutrientRecRegex.Match(header).Groups["RecNum"].Value; int columnIndex = Headers.IndexOf(header); RecommendationMappings.Add($"{columnName} Rec {recNum}", columnIndex); } if (FieldIdRegex.IsMatch(header)) { int columnIndex = Headers.IndexOf(header); FieldIndex = columnIndex; } if (YearRegex.IsMatch(header)) { int columnIndex = Headers.IndexOf(header); YearIndex = columnIndex; } } }
private SoilSampleNutrientModel CreateSampleNutrientRecord(List <string> sample, string key) { int index = NutrientMappings[key]; SoilSampleNutrientModel newNutrientRecord = new SoilSampleNutrientModel(); newNutrientRecord.Amount = null; newNutrientRecord.Recommendation = null; newNutrientRecord.NutrientId = Convert.ToInt32(key); NutrientModel nutrient = GlobalConfig.Connection.GetNutrient_ById(newNutrientRecord.NutrientId); if (sample[index] != "NULL") { newNutrientRecord.Amount = Convert.ToDecimal(sample[index]); string nutrientUnit = Units.Where <UnitModel>(unit => unit.Id == nutrient.UnitId).Select(unit => unit.Unit).First(); //Makes sure it's a quantity that needs to be converted if (nutrientUnit != "None") { newNutrientRecord.Amount *= SoilSampleNutrientModel.PPMConversionFactor; } } Regex recommendationRegex = new Regex($"^{key}\\sRec\\s\\d"); var recommendationColumns = RecommendationMappings .Where(pair => recommendationRegex.IsMatch(pair.Key)) .Select(pair => pair.Value); if (HasRecommendations(recommendationColumns)) { decimal totalRec = recommendationColumns .Where(i => decimal.TryParse(sample[i], out decimal recommendation)) .Select(i => decimal.Parse(sample[i])) .Sum(); if (NotAllNull(sample, recommendationColumns)) { newNutrientRecord.Recommendation = totalRec; } } return(newNutrientRecord); }