Print Page | Close Window

Combining Filters in QueryInterceptor

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4291
Printed Date: 03-Apr-2025 at 2:43pm


Topic: Combining Filters in QueryInterceptor
Posted By: gregweb
Subject: Combining Filters in QueryInterceptor
Date Posted: 25-Aug-2013 at 2:12pm
In my QueryInterceptor, I add several filters. The issue is that only one filter is allowed.

So I am trying to check and see if an existing filter exists, and if it does, add a second PredicateDescription to it.

    if (typeof(IDeletable).IsAssignableFrom(Query.QueryableType)) {
               var deleteFilter = new PredicateDescription(Query.QueryableType, "IsDeleted", FilterOperator.IsEqualTo, false);
               var existingFilter = QueryFilters.GetFilterQueryForType(Query.QueryableType);
               if (existingFilter != null) {
                    var combinedFilter = //Combine Filters Here
                    QueryFilters.AddFilter(combinedFilter, true);
               }
               else {
                    QueryFilters.AddFilter(deleteFilter);
               }       
            }

But I don't see a way to do this.

Greg



Replies:
Posted By: kimj
Date Posted: 26-Aug-2013 at 7:20pm
It might be easiest to create and combine the filters, and then add the combined filter to the QueryFilters.
 
var filters = new List<PredicateDescription>();
 
// Some conditional logic ..
filters.Add(new PredicateDescription(Query.QueryableType, "SomeProp", FilterOperator.IsEqualTo, "a"));
 
// More conditional logic ..
filters.Add(new PredicateDescription(Query.QueryableType, "Discontinued", FilterOperator.IsEqualTo, false));
 
// Combine and add
var combinedFilter = PredicateBuilder.And(filters.ToArray());
QueryFilters.AddFilter(combinedFilter);
 


Posted By: gregweb
Date Posted: 22-Sep-2013 at 6:55pm
Thanks, great idea.

Greg



Print Page | Close Window