protected virtual object GetDataSourceValue(DataTable table, DataRow row, CancellationToken cancel, IProgress <ExecutionProgress> progress) { string filterExpression = string.Empty; for (int criteriaIndex = 0; criteriaIndex < LookupCriteria.Count; criteriaIndex++) { LookupCriteria lookupCriteria = LookupCriteria[criteriaIndex]; object filterValue = lookupCriteria.GetValue(row, cancel, progress); if (filterValue == null) { return(null); // todo - we can't look up a null value as part of a lookup at this stage } if (filterValue is string) { filterValue = ((string)filterValue).Replace("'", "''"); } if (filterValue is string || filterValue is Guid || filterValue is DateTime) { filterValue = string.Concat("'", filterValue, "'"); } filterExpression = string.Concat(filterExpression, string.Format("{0}{1}{2}{3}", criteriaIndex == 0 ? string.Empty : " AND ", lookupCriteria.SourceField.ColumnName, lookupCriteria.ComparisonOperator == ComparisonOperator.DoesNotEqual ? "<>" : "=", filterValue)); } string queryDescription = string.Format("{0} records with {1}", DataSource.Name, filterExpression); string sort = string.Empty; if (OrderByField != default(DataTableField)) { sort = string.Format("{0} {1}", OrderByField.ColumnName, ColumnOrder == ColumnOrder.Ascending ? "ASC" : "DESC"); } object value = default(object); List <DataRow> rows = table.Select(filterExpression, sort).ToList(); // todo - could cause error if data table is null if (rows.Count == 1 || (rows.Count > 1 && DuplicateBehaviour == DuplicateBehaviour.First)) { value = Convert(rows[0][ValueField.ColumnName]); } else if (rows.Count > 1 && DuplicateBehaviour == DuplicateBehaviour.Error) { throw new DuplicateLookupValueException(string.Format("Matched {0} {1}", rows.Count, queryDescription), this); } else if (rows.Count == 0 && NotFoundBehaviour == NotFoundBehaviour.Error) { throw new MissingLookupValueException(string.Format("No {0} could be found", queryDescription), this); } return(value); }
public virtual LookupCriteria Clone() { LookupCriteria clone = (LookupCriteria)MemberwiseClone(); return(clone); }