internal SingleFilterInfo ParseFilter(string filterPart) { string[] filterStringParts; SingleFilterInfo filterInfo = new SingleFilterInfo(); filterInfo.OperatorValue = DetermineFilterOperator(filterPart); switch (filterInfo.OperatorValue) { case FilterOperator.GreaterThan: filterStringParts = filterPart.Split(new string[] { ">" }, StringSplitOptions.RemoveEmptyEntries); break; case FilterOperator.LessThan: filterStringParts = filterPart.Split(new string[] { "<" }, StringSplitOptions.RemoveEmptyEntries); break; case FilterOperator.EqualTo: filterStringParts = filterPart.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries); break; case FilterOperator.Like: filterStringParts = filterPart.Split(new string[] { filterInfo.OperatorValue.ToString() }, StringSplitOptions.RemoveEmptyEntries); break; default: throw new ArgumentOutOfRangeException("Filter Operator", "No operator was found. Valid operators are >[=], <[=], = and 'Like'."); } filterInfo.PropName = filterStringParts[0].Replace("[", "").Replace("]", "").Replace(" AND ", "").Trim(); // Get the property descriptor for the filter property name. PropertyDescriptor filterPropDesc = TypeDescriptor.GetProperties(typeof(T))[filterInfo.PropName]; // Convert the filter compare value to the property type. if (filterPropDesc == null) { throw new InvalidOperationException("Specified property to filter " + filterInfo.PropName + " on does not exist on type: " + typeof(T).Name); } filterInfo.PropDesc = filterPropDesc; string comparePartNoQuotes = StripOffQuotes(filterStringParts[1]); try { TypeConverter converter = TypeDescriptor.GetConverter(filterPropDesc.PropertyType); filterInfo.CompareValue = converter.ConvertFromString(comparePartNoQuotes); } catch (NotSupportedException) { throw new InvalidOperationException("Specified filter value " + comparePartNoQuotes + " can not be converted" + "from string. Implement a type converter for " + filterPropDesc.PropertyType.ToString()); } return(filterInfo); }
internal void ApplyFilter(SingleFilterInfo filterParts) { List <T> results; // Check to see if the property type we are filtering by implements the IComparable interface. Type interfaceType = TypeDescriptor.GetProperties(typeof(T))[filterParts.PropName].PropertyType.GetInterface("IComparable"); if (interfaceType == null) { throw new InvalidOperationException("Filtered property must implement IComparable."); } results = new List <T>(); // Check each value and add to the results list. foreach (T item in this) { if (filterParts.PropDesc.GetValue(item) != null) { IComparable compareValue; if (filterParts.PropName == "Employers") { // probably need to find the exact employer here. and then implement icomparabel on that particular employer compareValue = (IComparable)filterParts.PropDesc.GetValue(item); EmployersDefendant emps = (EmployersDefendant)filterParts.PropDesc.GetValue(item); foreach (EmployerDefendant emp in emps) { if (string.IsNullOrEmpty(emp.SeparationDate)) { if (emp.EmployerName.StartsWith(filterParts.CompareValue.ToString(), StringComparison.CurrentCultureIgnoreCase)) { results.Add(item); break; } } } } else { compareValue = (IComparable)filterParts.PropDesc.GetValue(item); // like operator works differently than the >, < and = operators, so handle it by itself. if (filterParts.OperatorValue == FilterOperator.Like) { if (compareValue.ToString().StartsWith(filterParts.CompareValue.ToString(), StringComparison.CurrentCultureIgnoreCase)) { results.Add(item); } } else { int result = compareValue.CompareTo(filterParts.CompareValue); if (filterParts.OperatorValue == FilterOperator.EqualTo && result == 0) { results.Add(item); } else if (filterParts.OperatorValue == FilterOperator.GreaterThan && result > 0) { results.Add(item); } else if (filterParts.OperatorValue == FilterOperator.LessThan && result < 0) { results.Add(item); } } //if( compareValue.ToString().StartsWith( filterParts.CompareValue.ToString(), StringComparison.CurrentCultureIgnoreCase ) ) //{ // results.Add( item ); //} } } } // clearing the current list and adding the found results back in this.ClearItems(); foreach (T itemFound in results) { this.Add(itemFound); } }