Phil
Phil

Reputation: 4224

RavenDB - Build a dynamic query with OR filters

I need to be able to build a query at runtime that uses OR statments. If I use the method below to build the query, everything is AND together. I really need each filter value to be OR in order for this query to work correctly.

public class IdAndRole
{
    public string Id {get;set;}
    public string Role {get;set;}
}

var idAndRoles = session.Query<IdAndRole, Roles_ById>();
foreach(var filter in filterValues)
{
    idAndRoles = idAndRoles.Where(x => x.In(filter.Id) && x.In(filter.Role));
}

Pseudocode:

(filter[0].Id == value1 && filter[0].Role == role1) ||(filter[1].Id == value2 && filter[1].Role == role2)

Upvotes: 3

Views: 1084

Answers (2)

Ayende Rahien
Ayende Rahien

Reputation: 22956

Phil, You can drop down into the LuceneQuery, and that allows you fine grained control over your query.

Upvotes: 2

tvanfosson
tvanfosson

Reputation: 532435

You should be able to use a PredicateBuilder to construct the query.

var predicate = PredicateBuilder.False<IdAndRole>();
foreach (var filter in filterValues)
{
     predicate = predicate.Or( x => x.In(filter.Id) && x.In(filter.Role) );
}

var idAndRoles = session.Query<IdAndRole,Roles_byId>()
                        .Where( predicate );

Upvotes: 5

Related Questions