1
Vote

Correction on filtering to accept OR logic

description

Hi,
Filtering was not working when filtering this way (on column "ElapseDays"): (ElapseDays~lt~1~or~(ElapseDays~gt~1~and~ElapseDays~lt~5))

Can you please modify code and use only one ExpressionWhereClause for each column :
private ExpressionWhereClause<TEntity, TViewModel> ApplyFilteringWithExpressionWhereClause(ExpressionWhereClause<TEntity, TViewModel> dynamicWhereClause, IList<IFilterDescriptor> filterDescriptors, FilterCompositionLogicalOperator logicalOperator = FilterCompositionLogicalOperator.And)
        {
            if (filterDescriptors.Any())
            {
                foreach (var filterDescriptor in filterDescriptors.Reverse())
                {
                    if (filterDescriptor is CompositeFilterDescriptor)
                    {
                        var compositeFilterDescriptor = filterDescriptor as CompositeFilterDescriptor;
                        dynamicWhereClause = ApplyFilteringWithExpressionWhereClause(dynamicWhereClause, compositeFilterDescriptor.FilterDescriptors, compositeFilterDescriptor.LogicalOperator);
                    }
                    else
                    {
                        var filter = (FilterDescriptor)filterDescriptor;
                        var whereClause = new ExpressionWhereClause<TEntity, TViewModel>(filter, CaseInsensitiveField, AcceptNullValuesWhenFilteringField, QueryValueResolvers);

                        if (logicalOperator == FilterCompositionLogicalOperator.And)
                        {
                            dynamicWhereClause.AndAlso(whereClause);
                        }
                        else
                        {
                            dynamicWhereClause.OrElse(whereClause);
                        }
                    }
                }
            }

            return dynamicWhereClause;
        }



        private IQueryable<TEntity> ApplyFilteringWithExpressionWhereClause(IQueryable<TEntity> data, IList<IFilterDescriptor> filterDescriptors, FilterCompositionLogicalOperator logicalOperator = FilterCompositionLogicalOperator.And)
        {
            if (filterDescriptors.Any())
            {
                var dynamicWhereClause = new ExpressionWhereClause<TEntity, TViewModel>();

                foreach (var filterDescriptor in filterDescriptors.Reverse())
                {
                    if (filterDescriptor is CompositeFilterDescriptor)
                    {
                        var compositeFilterDescriptor = filterDescriptor as CompositeFilterDescriptor;
                        dynamicWhereClause = ApplyFilteringWithExpressionWhereClause(dynamicWhereClause, compositeFilterDescriptor.FilterDescriptors, compositeFilterDescriptor.LogicalOperator);
                    }
                    else
                    {
                        var filter = (FilterDescriptor)filterDescriptor;
                        var whereClause = new ExpressionWhereClause<TEntity, TViewModel>(filter, CaseInsensitiveField, AcceptNullValuesWhenFilteringField, QueryValueResolvers);

                        if (logicalOperator == FilterCompositionLogicalOperator.And)
                        {
                            dynamicWhereClause.AndAlso(whereClause);
                        }
                        else
                        {
                            dynamicWhereClause.OrElse(whereClause);
                        }
                    }
                }

                if (dynamicWhereClause.HasFilters())
                {
                    data = data.Where(dynamicWhereClause.Predicate);
                }
            }

            return data;
        }
  • Frédéric

comments