/// <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); }