public static bool HasOperator(FilterOperatorField allOperators, FilterOperatorField singleOperator) { return((allOperators & singleOperator) == singleOperator); }
/// <summary> /// Compares two generic property values according to the filter operator field. /// Is used to search business entity collections by reflection /// </summary> /// <typeparam name="T">Type of property (must implement IComparable)</typeparam> /// <param name="filterValue">Filtering value</param> /// <param name="propertyValue">Value of a property (of a given business entity)</param> /// <param name="filterOperator">Filter criterion</param> /// <returns></returns> public static bool PropertyValuesMatch <T>(T filterValue, T propertyValue, FilterOperatorField filterOperator) where T : IComparable { bool result = false; // If either arg is null then the only available options will be equal or not equal if (propertyValue == null || filterValue == null) { switch (filterOperator) { case FilterOperatorField.NotEqual: result = (propertyValue == null && filterValue != null) || (propertyValue != null && filterValue == null); break; case FilterOperatorField.Equal: result = propertyValue == null && filterValue == null; break; } return(result); } // Normal, non-null arguments switch (filterOperator) { case FilterOperatorField.NotEqual: if (typeof(T).IsAssignableFrom(typeof(string))) { string filterString = filterValue as string; string propertyString = propertyValue as string; result = propertyString.ToLower() != filterString.ToLower(); } else { result = !propertyValue.Equals(filterValue); } break; case FilterOperatorField.Equal: if (typeof(T).IsAssignableFrom(typeof(string))) { string filterString = filterValue as string; string propertyString = propertyValue as string; result = propertyString.ToLower() == filterString.ToLower(); } else { result = propertyValue.Equals(filterValue); } break; case FilterOperatorField.GreaterThan: result = propertyValue.CompareTo(filterValue) < 0; break; case FilterOperatorField.GreaterThanOrEqual: result = propertyValue.CompareTo(filterValue) <= 0; break; case FilterOperatorField.LessThan: result = propertyValue.CompareTo(filterValue) > 0; break; case FilterOperatorField.LessThanOrEqual: result = propertyValue.CompareTo(filterValue) > 0; break; case FilterOperatorField.Contains: if (typeof(T).IsAssignableFrom(typeof(string))) { string filterString = filterValue as string; string propertyString = propertyValue as string; result = propertyString.ToLower().Contains(filterString.ToLower()); } break; case FilterOperatorField.StartsWith: if (typeof(T).IsAssignableFrom(typeof(string))) { string filterString = filterValue as string; string propertyString = propertyValue as string; result = propertyString.ToLower().StartsWith(filterString.ToLower()); } break; } return(result); }