public static Dictionary <ProductItemType, bool> GetSelectedProductItemTypeValues(Dictionary <ProductItemType, bool> dbValues, ProductItemType[] values, string currentValues) { if (values.Any()) { dbValues = dbValues.Select(s => values.Contains(s.Key) ? new KeyValuePair <ProductItemType, bool>(s.Key, true) : s).ToDictionary(pair => pair.Key, pair => pair.Value); } else { if (currentValues != default) { ICollection <ProductItemType> listOfTypes = new List <ProductItemType>(); foreach (var type in currentValues.Split(",")) { if (ProductItemType.TryParse(type, out ProductItemType result)) { listOfTypes.Add(result); } } dbValues = dbValues.Select(s => listOfTypes.Contains(s.Key) ? new KeyValuePair <ProductItemType, bool>(s.Key, true) : s).ToDictionary(pair => pair.Key, pair => pair.Value); } } return(dbValues); }