Skip to main content

Filter Query Results

Where

IList<Employee> results = session
.Query<Employee, Employees_ByFirstAndLastName>() // query 'Employees/ByFirstAndLastName' index
.Where(x => x.FirstName == "Robert" && x.LastName == "King") // filtering predicates
.ToList(); // materialize query by sending it to server for processing

Where - Numeric Property

IList<Product> results = session
.Query<Product, Products_ByUnitsInStock>() // query 'Products/ByUnitsInStock' index
.Where(x => x.UnitsInStock > 50) // filtering predicates
.ToList(); // materialize query by sending it to server for processing

Where - Nested Property

// return all orders that were shipped to 'Albuquerque'
IList<Order> results = session
.Query<Order>()
.Where(x => x.ShipTo.City == "Albuquerque")
.ToList();
IList<Order> results = session
.Query<Order, Order_ByOrderLinesCount>() // query 'Order/ByOrderLinesCount' index
.Where(x => x.Lines.Count > 50) // filtering predicates
.ToList(); // materialize query by sending it to server for processing

Where + Any

Any is useful when you have a collection of items (e.g. Order contains OrderLines) and you want to filter out based on values from this collection. For example, let's retrieve all orders that contain an OrderLine with a given product.

IList<Order> results = session
.Query<Order, Order_ByOrderLines_ProductName>() // query 'Order/ByOrderLines/ProductName' index
.Where(x => x.Lines.Any(l => l.ProductName == "Teatime Chocolate Biscuits")) // filtering predicates
.ToList(); // materialize query by sending it to server for processing

Where + In

When you want to check a single value against multiple values, the In operator can become handy. To retrieve all employees where FirstName is either Robert or Nancy, we can issue the following query:

IList<Employee> results = session
.Query<Employee, Employees_ByFirstAndLastName>() // query 'Employees/ByFirstAndLastName' index
.Where(x => x.FirstName.In("Robert", "Nancy")) // filtering predicates (remember to add `Raven.Client.Linq` namespace to usings)
.ToList(); // materialize query by sending it to server for processing

Remember to add the Raven.Client.Documents.Linq namespace to usings if you want to use In extension method.

Where + ContainsAny

To check if enumeration contains any of the values from a specified collection, you can use the ContainsAny method.

Let's assume that we want to return all BlogPosts that contain any of the specified Tags.

IList<BlogPost> results = session
.Query<BlogPost, BlogPosts_ByTags>() // query 'BlogPosts/ByTags' index
.Where(x => x.Tags.ContainsAny(new[] { "Development", "Research" })) // filtering predicates (remember to add `Raven.Client.Linq` namespace to usings)
.ToList(); // materialize query by sending it to server for processing

Remember to add the Raven.Client.Documents.Linq namespace to usings if you want to use the ContainsAny extension method.

Where + ContainsAll

To check if an enumeration contains all of the values from a specified collection, you can use the ContainsAll method.

Let's assume that we want to return all the BlogPosts that contain all of the specified Tags.

IList<BlogPost> results = session
.Query<BlogPost, BlogPosts_ByTags>() // query 'BlogPosts/ByTags' index
.Where(x => x.Tags.ContainsAll(new[] { "Development", "Research" })) // filtering predicates (remember to add `Raven.Client.Linq` namespace to usings)
.ToList(); // materialize query by sending it to server for processing

Remember to add the Raven.Client.Documents.Linq namespace to usings if you want to use the ContainsAll extension method.

Where - StartsWith

// return all products which name starts with 'ch'
IList<Product> results = session
.Query<Product>()
.Where(x => x.Name.StartsWith("ch"))
.ToList();

Where - EndsWith

// return all products which name ends with 'ra'
IList<Product> results = session
.Query<Product>()
.Where(x => x.Name.EndsWith("ra"))
.ToList();

Where - Identifier Property

Once a property used in the Where clause is recognized as an identity property of a given entity type (according to FindIdentityProperty convention) and there aren't any other fields involved in the query, then it is called a "collection query". Simple collection queries that ask about documents with given IDs or where identifiers start with a given prefix and don't require any additional handling like ordering, full-text searching, etc, are handled directly by the storage engine. It means that querying by ID doesn't create an auto-index and has no extra cost. In terms of efficiency, it is the same as loading documents with session.Load usage.

Order order = session
.Query<Order>()
.Where(x => x.Id == "orders/1-A")
.FirstOrDefault();
IList<Order> orders = session
.Query<Order>()
.Where(x => x.Id.StartsWith("orders/1"))
.ToList();

Where - Exists

To find all documents in a collection that have a specified field, see How to Filter by Field Presence.

To find all documents in a collection that don't have a specified field, see How to Filter by Non-Existing Field.

Remarks

Query and DocumentQuery are converting predicates to the IndexQuery class so they can issue a query from a low-level operation method.