/// <summary> /// Gets the Linq.Expression that will represent the AdvancedPaging query /// </summary> /// <param name="model">The AdvancedPageModel</param> /// <param name="genericType">The ParameterExpression that represents the Entity</param> /// <returns>The Expression for the AdvancedPage</returns> public Expression GetAdvancedSearchRestrictions(AdvancedPageModel model, ParameterExpression genericType) { Expression restrictions = null; if (model.AdvancedSearch == null) { return(restrictions); } foreach (var adv in model.AdvancedSearch) { var valueA = (object)(adv.IntValue.HasValue ? adv.IntValue.Value : adv.Value); var key = typeof(TEntity).GetPropertyExpressionFromSubProperty(adv.PropertyName, genericType); //if (key.Type == typeof(int)) //{ // key = ((MemberExpression)key).ConvertToType(TypeCode.String); //}; var propertyType = typeof(TEntity).FollowPropertyPath(adv.PropertyName).PropertyType; var value = valueA != null?Expression.Constant(RepositoryExtensions.ChangeType(valueA, propertyType)) : null; Expression addedExpression = null; switch (adv.TypeOfSearch) { case AdvancedSearchType.IsNull: addedExpression = RepositoryExtensions.NullableEqual(key, Expression.Constant(null)); break; case AdvancedSearchType.IsNotNull: addedExpression = RepositoryExtensions.NullableNotEqual(key, Expression.Constant(null)); break; case AdvancedSearchType.In: addedExpression = RepositoryExtensions.InExpression <TEntity>(genericType, adv.PropertyName, adv.ListValue); break; case AdvancedSearchType.NotIn: addedExpression = Expression.Not(RepositoryExtensions.InExpression <TEntity>(genericType, adv.PropertyName, adv.ListValue)); break; case AdvancedSearchType.Equal: addedExpression = RepositoryExtensions.NullableEqual(key, value); break; case AdvancedSearchType.NotEqual: addedExpression = RepositoryExtensions.NullableNotEqual(key, value); break; case AdvancedSearchType.LessThan: addedExpression = RepositoryExtensions.NullableLessThan(key, value); break; case AdvancedSearchType.LessThanEqual: addedExpression = RepositoryExtensions.NullableLessThanOrEqualTo(key, value); break; case AdvancedSearchType.GreaterThan: addedExpression = RepositoryExtensions.NullableGreaterThan(key, value); break; case AdvancedSearchType.GreaterThanEqual: addedExpression = RepositoryExtensions.NullableGreaterThanOrEqualTo(key, value); break; case AdvancedSearchType.Between: var lowerBound = Expression.GreaterThanOrEqual(key, Expression.Constant(Convert.ChangeType(adv.Value, propertyType))); var upperBound = Expression.LessThanOrEqual(key, Expression.Constant(Convert.ChangeType(adv.Value2, propertyType))); addedExpression = Expression.AndAlso(lowerBound, upperBound); break; case AdvancedSearchType.NotLike: addedExpression = Expression.Not(RepositoryExtensions.Contains(key, valueA)); break; case AdvancedSearchType.Like: default: addedExpression = RepositoryExtensions.Contains(key, valueA); break; } //add the new expression to the list restrictions = restrictions == null ? addedExpression : Expression.AndAlso(restrictions, addedExpression); } return(restrictions); }
/// <summary> /// Creates an Linq.Expression for the Searchable properties /// </summary> /// <param name="searchCriteria">The search string</param> /// <param name="genericType">The ParameterExpression that represents the Entity</param> /// <returns>Expression</returns> public Expression GetSearchRestrictions(object searchCriteria, ParameterExpression genericType) { Expression restrictions = null; if (searchCriteria != null) { foreach (var sc in typeof(TEntity).GetProperties()) { if (sc.IsDefined(typeof(SearchAbleAttribute), true)) { var searchAtts = (SearchAbleAttribute[])sc.GetCustomAttributes(typeof(SearchAbleAttribute), true); //walk each searchable attribute on the property foreach (var att in searchAtts) { var propertyName = string.IsNullOrEmpty(att.AliasName) ? sc.Name : att.AliasName; var propertyType = typeof(TEntity).FollowPropertyPath(propertyName).PropertyType; //check for special cases where a string cannot be converted to the type specifically if (FieldCanBeSearch(propertyType, searchCriteria)) { var key = typeof(TEntity).GetPropertyExpressionFromSubProperty(propertyName, genericType); var value = Expression.Constant(RepositoryExtensions.ChangeType(searchCriteria, propertyType)); Expression addedExpression = null; switch (att.SearchType) { case SearchAbleType.Equal: addedExpression = RepositoryExtensions.NullableEqual(key, value); break; case SearchAbleType.NotEqual: addedExpression = RepositoryExtensions.NullableNotEqual(key, value); break; case SearchAbleType.GreaterThan: addedExpression = RepositoryExtensions.NullableGreaterThan(key, value); break; case SearchAbleType.GreaterThanEqual: addedExpression = RepositoryExtensions.NullableGreaterThanOrEqualTo(key, value); break; case SearchAbleType.LessThan: addedExpression = RepositoryExtensions.NullableLessThan(key, value); break; case SearchAbleType.LessThanEqual: addedExpression = RepositoryExtensions.NullableLessThanOrEqualTo(key, value); break; case SearchAbleType.Contains: var method = typeof(string).GetMethod("Contains", new[] { typeof(string) }); var someValue = Expression.Constant(searchCriteria, typeof(string)); addedExpression = Expression.Call(key, method, someValue); break; case SearchAbleType.StartsWith: var methodsw = typeof(string).GetMethod("StartsWith", new[] { typeof(string) }); var swValue = Expression.Constant(searchCriteria, typeof(string)); addedExpression = Expression.Call(key, methodsw, swValue); break; case SearchAbleType.EndsWith: var methodew = typeof(string).GetMethod("EndsWith", new[] { typeof(string) }); var ewValue = Expression.Constant(searchCriteria, typeof(string)); addedExpression = Expression.Call(key, methodew, ewValue); break; } //add the new expression to the list of restrictions restrictions = restrictions == null ? addedExpression : Expression.OrElse(restrictions, addedExpression); } } } } } return(restrictions); }