Skip to main content

Session: Querying: How to Filter by Field Presence

  • To filter documents by whether they contain a particular field, use the LINQ extension method WhereExists() accessible from the Document Query.

  • If a document doesn't contain the specified field, it is excluded from the query results.

  • In this page:

Syntax

// Only documents that contain field 'FirstName' will be returned

List<Employee> results = session
.Advanced
.DocumentQuery<Employee>()
.WhereExists("FirstName")
// Or use lambda expression: .WhereExists(x => x.FirstName)
.ToList();
ParametersTypeDescription
fieldNamestringThe name of the field you want to filter by
propertySelectorExpression<Func<T,TValue>>The path to the field you want to filter by

Examples

Filter by Field Name

List<Employee> results = session
.Advanced
.DocumentQuery<Employee>()
.WhereExists("FirstName")
.ToList();

Filter by the Path to a Field

This is done by passing the path as a lambda expression:

List<Employee> results = session
.Advanced
.DocumentQuery<Employee>()
.WhereExists(x => x.Address.Location.Latitude)
.ToList();

The path can also be passed as a string:

List<Employee> results = session
.Advanced
.DocumentQuery<Employee>()
.WhereExists("Address.Location.Latitude")
.ToList();