diff --git a/PxWeb/Code/Api2/DataSelection/SelectionHandler.cs b/PxWeb/Code/Api2/DataSelection/SelectionHandler.cs index f1249904..c6b97b8e 100644 --- a/PxWeb/Code/Api2/DataSelection/SelectionHandler.cs +++ b/PxWeb/Code/Api2/DataSelection/SelectionHandler.cs @@ -106,20 +106,16 @@ private static bool ExpandSelectionExpressionsAndVerifyValues(IPXModelBuilder bu { var modelVariable = model.Meta.Variables.GetByCode(variable.VariableCode); + var valueCodes = variable.ValueCodes.ToList(); - for (int i = 0; i < variable.ValueCodes.Count; i++) + foreach (var valueCode in valueCodes) { - var valueCode = variable.ValueCodes[i]; // Try to get the value using the code specified by the API user Value? pxValue = modelVariable.Values.FirstOrDefault(x => x.Code.Equals(valueCode, System.StringComparison.InvariantCultureIgnoreCase)); - if (pxValue is null) + if (!ExpandValue(ref problem, variable, modelVariable, valueCode, pxValue)) { - return ExpandSelectionExpression(variable, modelVariable, valueCode, out problem); - } - else - { - variable.ValueCodes[i] = pxValue.Code; + return false; } } @@ -135,6 +131,29 @@ private static bool ExpandSelectionExpressionsAndVerifyValues(IPXModelBuilder bu return true; } + private static bool ExpandValue(ref Problem? problem, VariableSelection variable, Variable modelVariable, string valueCode, Value? pxValue) + { + if (pxValue is null) + { + if (ExpandSelectionExpression(variable, modelVariable, valueCode, out problem)) + { + variable.ValueCodes.Remove(valueCode); + } + else + { + return false; + } + } + else + { + if (valueCode != pxValue.Code) + { + variable.ValueCodes[variable.ValueCodes.IndexOf(valueCode)] = pxValue.Code; + } + } + return true; + } + /// /// Expand selection expression and add to selection /// This method should only be