Navigation

Query Engine

Overview

To filter data in your realm, you can leverage Realm Database’s query engine. Realm Database’s query engine implements standard LINQ syntax. See the exact scope of LINQ implemented on the LINQ Support page.

Operators

There are several types of operators available to filter a Realm collection. Filters work by evaluating an operator expression for every object in the collection being filtered. If the expression resolves to true, Realm Database includes the object in the results collection.

An expression consists of one of the following:

  • The name of a property of the object currently being evaluated.
  • An operator and up to two argument expression(s).
  • A literal string, number, or date.

About the examples on this page

The examples in this page use a simple data set for a task list app. The two Realm object types are Project and Task. A Task has a name, assignee’s name, and completed flag. There is also an arbitrary number for priority – higher is more important – and a count of minutes spent working on it. A Project has zero or more Tasks.

See the schema for these two classes, Project and Task, below:

public class Task : RealmObject
{
    public string Name { get; set; }
    public string Assignee { get; set; }
    public bool IsComplete { get; set; }
    public int Priority { get; set; }
    public int ProgressMinutes { get; set; }
}

public class Project : RealmObject
{
    public string Name { get; set; }
    public List<Task> Tasks { get; set; }
}

Comparison Operators

The most straightforward operation in a search is to compare values.

Operator Description
==
Evaluates to true if the left-hand expression is equal to the right-hand expression.
>
Evaluates to true if the left-hand numerical or date expression is greater than the right-hand numerical or date expression. For dates, this evaluates to true if the left-hand date is later than the right-hand date.
>=
Evaluates to true if the left-hand numerical or date expression is greater than or equal to the right-hand numerical or date expression. For dates, this evaluates to true if the left-hand date is later than or the same as the right-hand date.
<
Evaluates to true if the left-hand numerical or date expression is less than the right-hand numerical or date expression. For dates, this evaluates to true if the left-hand date is earlier than the right-hand date.
<=
Evaluates to true if the left-hand numeric expression is less than or equal to the right-hand numeric expression. For dates, this evaluates to true if the left-hand date is earlier than or the same as the right-hand date.
!=
Evaluates to true if the left-hand expression is not equal to the right-hand expression.

Example

The following example uses the query engine’s comparison operators to:

  • Find high priority tasks by comparing the value of the priority property value with a threshold number, above which priority can be considered high.
  • Find just-started or short-running tasks by seeing if the progressMinutes property falls within a certain range.
  • Find unassigned tasks by finding tasks where the assignee property is equal to null.
  • Find tasks assigned to specific teammates Ali or Jamie by seeing if the assignee property is in a list of names.
Debug.WriteLine("High priority tasks: "
        + tasks.Where(t => t.Priority > 5));

Debug.WriteLine("Just started or short tasks: "
    + tasks.Where(t => 1 <= t.ProgressMinutes && t.ProgressMinutes < 15));

Debug.WriteLine("Unassigned tasks: "
    + tasks.Where(t => t.Assignee == null));

Debug.WriteLine("Ali or Jamie's tasks: "
    + tasks.Where(t => new List<string> { "Ali", "Jamie" }.Contains(t.Assignee)));

Logical Operators

You can make compound predicates using logical operators.

Operator Description
&&
Evaluates to true if both left-hand and right-hand expressions are true.
!
Negates the result of the given expression.
||
Evaluates to true if either expression returns true.

Example

We can use the query language’s logical operators to find all of Ali’s completed tasks. That is, we find all tasks where the assignee property value is equal to ‘Ali’ AND the isComplete property value is true:

Debug.WriteLine("Ali's complete tasks: "
    + tasks.Where(t => t.Assignee == "Ali" && t.IsComplete));

String Operators

You can compare string values using these string operators. Regex-like wildcards allow more flexibility in search.

Operator Description
StartsWith Evaluates to true if the left-hand string expression begins with the right-hand string expression. This is similar to contains, but only matches if the left-hand string expression is found at the beginning of the right-hand string expression.
EndsWith
Evaluates to true if the left-hand string expression ends with the right-hand string expression. This is similar to contains, but only matches if the left-hand string expression is found at the very end of the right-hand string expression.
Like

Evaluates to true if the left-hand string expression matches the right-hand string wildcard string expression. A wildcard string expression is a string that uses normal characters with two special wildcard characters:

  • The * wildcard matches zero or more of any character
  • The ? wildcard matches any character.

For example, the wildcard string “d?g” matches “dog”, “dig”, and “dug”, but not “ding”, “dg”, or “a dog”.

Equals (C#)
Evaluates to true if the left-hand string is lexicographically equal to the right-hand string.

Example

We use the query engine’s string operators to find projects with a name starting with the letter ‘e’ and projects with names that contain ‘ie’:

bool ignoreCase = true;
Debug.WriteLine("Projects that start with 'e': "
    + projects.Where(p => p.Name.StartsWith("E", ignoreCase, StringComparison.CurrentCulture)));

Debug.WriteLine("Projects that contain 'ie': "
    + projects.Where(p => p.Name.Contains("ie", StringComparison.OrdinalIgnoreCase)));

Aggregate Operators

You can apply an aggregate operator to a collection property of a Realm object. Aggregate operators traverse a collection and reduce it to a single value.

Operator Description
Count (C#)
Evaluates to the number of objects in the given collection.

Example

We create a couple of filters to show different facets of the data:

  • Projects with average tasks priority above 5.
  • Long running projects.
Debug.WriteLine("Projects with average tasks priority above 5: "
    + projects.Where(p => p.Tasks.Average(task => task.Priority) > 5));\

Debug.WriteLine("Long running projects: "
    + projects.Where(p => p.Tasks.Sum(t => t.ProgressMinutes) > 120));

Summary

  • There are several categories of operators available to filter results: comparison, logical, string and aggregate operators.
←   Reads Threading  →