public virtual void AddFilterParam(FilterParam param) { throw new NotSupportedException(); }
private static IEnumerable <ObjectParameter> GetObjectParameters(FilterParam filterParam, int paramIndex, ref string filterQueryString) { List <ObjectParameter> objectParameters; var propertyName = filterParam.Property; var propertyValueParamName = propertyName.Replace('.', '_') + paramIndex; var propertyTargetValue = filterParam.Value; string comparisonOperator; switch (filterParam.Expression) { case DaoExpression.Eq: comparisonOperator = /*propertyTargetValue is IEnumerable ? "IN" :*/ "="; break; case DaoExpression.Gt: comparisonOperator = ">"; break; case DaoExpression.Lt: comparisonOperator = "<"; break; case DaoExpression.GtEq: comparisonOperator = ">="; break; case DaoExpression.LtEq: comparisonOperator = "<="; break; case DaoExpression.NotEq: comparisonOperator = "<>"; break; case DaoExpression.Contains: comparisonOperator = " LIKE "; propertyTargetValue = "%" + propertyTargetValue.ToString().Replace("%", "%%") + "%"; break; case DaoExpression.StartsWith: comparisonOperator = " LIKE "; propertyTargetValue = propertyTargetValue.ToString().Replace("%", "%%") + "%"; break; case DaoExpression.EndsWith: comparisonOperator = " LIKE "; propertyTargetValue = "%" + propertyTargetValue.ToString().Replace("%", "%%"); break; default: throw new NotSupportedException(filterParam.Expression + " is not supported"); } if (paramIndex > 0) { filterQueryString += " and "; } if (propertyTargetValue is ICollection) { objectParameters = new List <ObjectParameter>(); var valueCollection = (ICollection)propertyTargetValue; var valueIndex = 0; filterQueryString += "("; foreach (var value in valueCollection) { var collectionItemValueParamName = propertyValueParamName + "_" + valueIndex; if (valueIndex > 0) { filterQueryString += " or "; } filterQueryString += "it." + propertyName + comparisonOperator + "@" + collectionItemValueParamName; objectParameters.Add(new ObjectParameter(collectionItemValueParamName, value)); valueIndex++; } filterQueryString += ")"; } else { filterQueryString += "it." + propertyName + comparisonOperator + "@" + propertyValueParamName; objectParameters = new List <ObjectParameter> { new ObjectParameter(propertyValueParamName, propertyTargetValue) }; } return(objectParameters); }
public static IQueryable <T> ApplyFilters <T>(this IQueryable <T> elementCollection, FilterParam[] filters, SortParam sortParam) { var result = elementCollection; if (filters != null) { foreach (var filterParam in filters) { var prop = typeof(T).GetProperty(filterParam.Property); FilterParam param = filterParam; if (prop != null) { switch (filterParam.Expression) { //TODO: implement correct way of Contains/StartWith/EndWith case DaoExpression.Contains: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.Contains, param.Value); break; case DaoExpression.EndsWith: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.EndsWith, param.Value); break; case DaoExpression.StartsWith: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.StartsWith, param.Value); break; case DaoExpression.Eq: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.Equal, param.Value); break; case DaoExpression.Gt: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.GreaterThan, param.Value); break; case DaoExpression.GtEq: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.GreaterThanOrEqual, param.Value); break; case DaoExpression.Lt: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.LessThan, param.Value); break; case DaoExpression.LtEq: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.LessThanOrEqual, param.Value); break; case DaoExpression.NotEq: result = ApplyFilter(result, param.Property, ExtendedLinqExpressionType.NotEqual, param.Value); break; } } } } if (sortParam != null) { result = sortParam.Ascending ? result.OrderBy(sortParam.Property) : result.OrderByDescending(sortParam.Property); } else { result = result.OrderBy("Id"); } return(result); }