/// <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(LambdaHelpers.ChangeType(valueA, propertyType)) : null; Expression addedExpression = null; switch (adv.TypeOfSearch) { case AdvancedSearchType.IsNull: addedExpression = LambdaHelpers.NullableEqual(key, Expression.Constant(null)); break; case AdvancedSearchType.IsNotNull: addedExpression = LambdaHelpers.NullableNotEqual(key, Expression.Constant(null)); break; case AdvancedSearchType.In: addedExpression = LambdaHelpers.InExpression <TEntity>(genericType, adv.PropertyName, adv.ListValue); break; case AdvancedSearchType.Equal: addedExpression = LambdaHelpers.NullableEqual(key, value); break; case AdvancedSearchType.NotEqual: addedExpression = LambdaHelpers.NullableNotEqual(key, value); break; case AdvancedSearchType.LessThan: addedExpression = LambdaHelpers.NullableLessThan(key, value); break; case AdvancedSearchType.LessThanEqual: addedExpression = LambdaHelpers.NullableLessThanOrEqualTo(key, value); break; case AdvancedSearchType.GreaterThan: addedExpression = LambdaHelpers.NullableGreaterThan(key, value); break; case AdvancedSearchType.GreaterThanEqual: addedExpression = LambdaHelpers.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.Like: default: var method = typeof(string).GetMethod("Contains", new[] { typeof(string) }); var someValue = Expression.Constant(valueA, typeof(string)); addedExpression = Expression.Call(key, method, someValue); break; } //add the new expression to the list restrictions = restrictions == null ? addedExpression : Expression.AndAlso(restrictions, addedExpression); } return(restrictions); }