private Expression BuildBaseExpression(ParameterExpression parameter, FilterDescriptor filterDescriptor)
        {
            var leftSide = GetPropertyExpression(parameter, filterDescriptor.PropertyName);

            var underlyingType = Nullable.GetUnderlyingType(leftSide.Type);

            if (IsNullableType(underlyingType, filterDescriptor))
            {
                var hasValue = Expression.Property(leftSide, HasValueProperty);
                var value    = Expression.Property(leftSide, ValueProperty);
                var equality = Expression.Equal(value, GetConstantValueExpression(filterDescriptor.Value, underlyingType));
                return(Expression.AndAlso(hasValue, equality));
            }

            var right = GetConstantValueExpression(filterDescriptor.Value, leftSide.Type);

            return(Expression.Equal(leftSide, right));
        }
        private Expression BuildCollectionExpression(FilterDescriptor filterDescriptor, ParameterExpression parameterExpression)
        {
            var separatorIndex     = filterDescriptor.PropertyName.IndexOf(PropertySeparator, StringComparison.Ordinal);
            var collectionProperty = filterDescriptor.PropertyName.Substring(0, separatorIndex);
            var propertyName       = filterDescriptor.PropertyName.Substring(separatorIndex + 1);
            var propertyType       = GetCollectionGenericType(parameterExpression, collectionProperty);
            var listParameter      = Expression.Parameter(propertyType, CollectionParameterName);
            var lambda             =
                Expression.Lambda(
                    BuildPropertyExpression(
                        new FilterDescriptor {
                PropertyName = propertyName, Value = filterDescriptor.Value
            },
                        listParameter),
                    listParameter);

            var propertyExpression = GetPropertyExpression(parameterExpression, collectionProperty);
            var anyExpression      = BuildAnyExpression(propertyType, propertyExpression, lambda);

            return(AddNullCheck(parameterExpression, filterDescriptor.PropertyName, anyExpression));
        }
Пример #3
0
 private static bool FilterProvided(FilterDescriptor filterDescriptor)
 {
     return(filterDescriptor != null);
 }
Пример #4
0
        public ICollection <TClass> ApplyFilter <TClass>(ICollection <TClass> collection, FilterDescriptor filterDescriptor)
        {
            if (!CollectionHasValues(collection))
            {
                return(collection);
            }

            if (!FilterProvided(filterDescriptor))
            {
                return(collection);
            }

            MatchPropertyProvided <TClass>(filterDescriptor.PropertyName);
            if (!PropertyMatched())
            {
                return(collection);
            }

            filterDescriptor.PropertyName = PropertyName;
            var expression = _expressionBuilder.GetFilterExpression <TClass>(filterDescriptor);

            return(collection.Where(expression.Compile()).ToList());
        }
 private bool IsNullableType(Type underlyingType, FilterDescriptor filterDescriptor)
 {
     return(underlyingType != null && filterDescriptor.Value != null);
 }
        private Expression BuildPropertyExpression(FilterDescriptor filterDescriptor, ParameterExpression parameterExpression)
        {
            var propertyExpression = BuildBaseExpression(parameterExpression, filterDescriptor);

            return(AddNullCheck(parameterExpression, filterDescriptor.PropertyName, propertyExpression));
        }