public Guid?GetContextForInstanceSubValue(DatasetElement datasetElement, DatasetElementSub datasetSubElement, string instanceValue) { var datasetInstanceValue = DatasetInstanceValues.SingleOrDefault(div => div.DatasetElement.Id == datasetElement.Id); if (datasetInstanceValue != null && datasetInstanceValue.DatasetInstanceSubValues.Any()) { return(datasetInstanceValue.DatasetInstanceSubValues.Where(disv => disv.DatasetElementSub.Id == datasetSubElement.Id && disv.InstanceValue.Trim() == instanceValue.Trim()).Select(disv => disv.ContextValue).First()); } return(null); }
public DatasetInstanceSubValue SetInstanceSubValue(DatasetElementSub datasetElementSub, string instanceSubValue, Guid contextValue = default(Guid)) { datasetElementSub.Validate(instanceSubValue); var datasetInstanceValue = DatasetInstanceValues.SingleOrDefault(div => div.DatasetElement.Id == datasetElementSub.DatasetElement.Id); if (datasetInstanceValue == null) { if (datasetElementSub.DatasetElement.Field.FieldType.Id == (int)FieldTypes.Table) { datasetInstanceValue = SetInstanceValue(datasetElementSub.DatasetElement, "<<TABLE>>"); } else { throw new Exception("Cannot set an instance sub value without setting the parent instance value"); } } var datasetInstanceSubValue = datasetInstanceValue.DatasetInstanceSubValues.SingleOrDefault(disv => disv.DatasetElementSub.Id == datasetElementSub.Id && disv.ContextValue == contextValue); if (datasetInstanceSubValue == null) { if (!string.IsNullOrWhiteSpace(instanceSubValue)) { datasetInstanceSubValue = datasetInstanceValue.AddDatasetInstanceSubValue(datasetElementSub, instanceSubValue, contextValue); } else { if (datasetElementSub.Field.Mandatory) { throw new DatasetFieldSetException(datasetElementSub.Id.ToString(), string.Format("{0} is required", datasetElementSub.ElementName)); } } } else { if (string.IsNullOrWhiteSpace(instanceSubValue)) { if (datasetElementSub.Field.Mandatory) { throw new DatasetFieldSetException(datasetElementSub.Id.ToString(), string.Format("{0} is required", datasetElementSub.ElementName)); } datasetInstanceValue.DatasetInstanceSubValues.Remove(datasetInstanceSubValue); datasetInstanceSubValue = null; } else { datasetInstanceSubValue.InstanceValue = instanceSubValue; } } return(datasetInstanceSubValue); }