/// <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);
        }
        /// <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(LambdaHelpers.ChangeType(searchCriteria, propertyType));
                                Expression addedExpression = null;
                                switch (att.SearchType)
                                {
                                case SearchAbleType.Equal:
                                    addedExpression = LambdaHelpers.NullableEqual(key, value);
                                    break;

                                case SearchAbleType.NotEqual:
                                    addedExpression = LambdaHelpers.NullableNotEqual(key, value);
                                    break;

                                case SearchAbleType.GreaterThan:
                                    addedExpression = LambdaHelpers.NullableGreaterThan(key, value);
                                    break;

                                case SearchAbleType.GreaterThanEqual:
                                    addedExpression = LambdaHelpers.NullableGreaterThanOrEqualTo(key, value);
                                    break;

                                case SearchAbleType.LessThan:
                                    addedExpression = LambdaHelpers.NullableLessThan(key, value);
                                    break;

                                case SearchAbleType.LessThanEqual:
                                    addedExpression = LambdaHelpers.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);
        }