Realm Xamarin
v0.81.0
Realm for Xamarin
|
To make a query with Realm, you use the Realm.All<T>()
method to get a IQueryable<T>
instance. On this you can then apply the operators listed below.
Where
is supported. OfType
is not but it would be redundant as a query in Realm will always consist of a collection of the class initially specified.
Where
takes a predicate. To see the supported operations for predicates in Realm queries, refer to the Predicate Operations section.
Example:
OrderBy
, OrderByDescending
, ThenBy
and ThenByDescending
are all supported. Reverse
is not yet supported. Currently, you can only order by persisted properties on the class that you are querying. This means that dogs.OrderBy(dog => dog.Owner.FirstName)
and the like is not yet supported.
Example:
ToArray
, ToList
, ToDictionary
and ToLookup
are all supported. Cast
isn't, but it would be redundant as a query in Realm will always consist of a collection of the class initially specified.
Example:
All of the main element operators are supported:
First
and FirstOrDefault
Last
and LastOrDefault
Single
and SingleOrDefault
.These methods take an optional predicate. To see the supported operations for predicates in Realm queries, refer to the Predicate Operations section.
Access to a single element by an index is supported by ElementAt
and ElementAtOrDefault
.
Note that, as is standard C# behaviour of default(T)
, the variants ...OrDefault
return a single null RealmObject
if there is no matching element.
DefaultIfEmpty
is not yet supported.
Any
is supported.
All
and Contains
are not yet supported.
Any
takes an optional predicate. To see the supported operations for predicates in Realm queries, refer to the Predicate Operations section.
Count
is supported.
LongCount
, Sum
, Min
, Max
and Average
are not yet supported.
Count
takes an optional predicate. To see the supported operations for predicates in Realm queries, refer to the Predicate Operations section.
As a general rule, you can only create predicates with conditions that rely on data in Realm. Imagine a class
Given this class, you can create queries with conditions that apply to the FirstName
and LastName
properties but not to the FullName
property. Likewise, methods, public fields and properties with the [Ignored]
attribute cannot be used.
Note that currently, the property must be the left side of a condition. This means that
is illegal and would have to be changed into the equivalent
Equality operators can be applied to all property types: ==
, !=
Furthermore, the following can be used for numerical types: <
, <=
, >
, >=
With strings, you can use: Contains
, StartsWith
and EndsWith
. Currently, only case sensitive comparisons are supported.
Example:
You can use parentheses and the ||
and &&
operators to compose queries.
Example:
Realm queries are live, in the sense that they will continue to represent the current state of the database.
This differs from the typical behavior of object/relational mappers (ORM's) where the result of a query is fetched and kept in memory as it was.
However, it also differs from the behavior of LINQ to Objects, where every iteration will reevaluate expressions, meaning that changes to both sides of a condition will affect the result. A Realm query will evaluate the right-hand sides of the conditions on the first run. So imagine you have a query like this:
Here, the recentLogEntries
variable will contain all the log entries that have a TimeStamp
later than one hour before the time when the query was first run (via foreach
, ToList
etc.) Newly added log entries will be included on subsequent runs, but the time they are compared to will not be updated.
The following features are not yet supported. A few of them will not be supported as the Realm architecture renders them unnecessary.
GroupBy
is not supported.
Distinct
, Union
, Intersect
and Except
are not supported.
Take
, Skip
, TakeWhile
and SkipWhile
are not yet supported.
These are less important than when using an ORM. Given Realm's zero-copy pattern, data is only read from the database when the properties on the objects are accessed, so there is little overhead in simply iterating over a part of a result.
Select
and SelectMany
are not yet supported.
The select
keyword used with the query syntax is supported as long as you select the RealmObject
itself and not some derivative:
Concat
is not supported.
Join
and GroupJoin
are not supported.
Note that joins are less vital when using Realm than when using a relational database and an ORM. Instead of using keys to identify relations, you simply refer to the related object.
So given a class
you can simply use that in another class like so:
This works like an ordinary reference in C# which means that if two Customer
instances are assigned the same Address
object, changes to that address will apply to both customer objects.