RavenDB - Optional where where

I want to write a RavenDB query that filters a value if it is available, but if that value is not available, I want it to return all objects. For example, in linq for objects, I can do something like this:

var matches = people.Where(x => x.LastName == userEntry || userEntry == string.Empty).ToList(); 

But the following will not work:

 var matches = RavenSession.Query<Person>().Where(x => x.LastName == userEntry || userEntry == string.Empty).ToList(); 

Since userEntry not an indexed value, this throws an exception.

How can i do this?

+8
c # ravendb
source share
1 answer

Based on your comment on a few optional predicates, you should do something like this:

 var where = new List<Expression<Func<Person, bool>>>(); if (!string.IsNullOrWhitespace(lastName)) where.Add(p => p.LastName == lastName); if (!string.IsNullOrWhitespace(firstName)) where.Add(p => p.FirstName == firstName); // etc... var query = session.Query<Person>(); foreach (var clause in where) query = query.Where(clause); var results = query.ToList(); 
+9
source share

All Articles