Skip to main content

Session: Querying: How to Use NOT Operator

IDocumentQuery contains NOT operator which can be used to negate next predicate

NOT operator cannot be used alone without succeeding predicate.

Example I

// load all entities from 'Employees' collection
// where FirstName NOT equals 'Robert'
List<Employee> employees = session
.Advanced
.DocumentQuery<Employee>()
.Not
.WhereEquals(x => x.FirstName, "Robert")
.ToList();

Example II

// load all entities from 'Employees' collection
// where FirstName NOT equals 'Robert'
// and LastName NOT equals 'King'
List<Employee> employees = session
.Advanced
.DocumentQuery<Employee>()
.Not
.OpenSubclause()
.WhereEquals(x => x.FirstName, "Robert")
.AndAlso()
.WhereEquals(x => x.LastName, "King")
.CloseSubclause()
.ToList();

Example III

// load all entities from 'Employees' collection
// where FirstName NOT equals 'Robert'
// and LastName NOT equals 'King'
// identical to 'Example II' but 'WhereNotEquals' is used
List<Employee> employees = session
.Advanced
.DocumentQuery<Employee>()
.WhereNotEquals(x => x.FirstName, "Robert")
.AndAlso()
.WhereNotEquals(x => x.LastName, "King")
.ToList();