Search code examples
.netrealm

Realm dotnet - The rhs of the binary operator 'Equal' should be a constant or closure variable expression


Hi have just started using Realm dotnet

When I perform a simple query like

var results = realm.All<MyRealmType>().Where(x => x.Property == otherVariable.Property);

So in the Where clause I am comparing two strings to retrieve the data I need from the realm.

I get the following error

{System.NotSupportedException: The rhs of the binary operator 'Equal' should be a constant or closure variable expression
  at Realms.RealmResultsVisitor.VisitBinary (System.Linq.Expressions.BinaryExpression b) [0x000cb] in <filename unknown>:0 
  at Realms.ExpressionVisitor.Visit (System.Linq.Expressions.Expression exp) [0x000d2] in <filename unknown>:0 
  at Realms.RealmResultsVisitor.VisitMethodCall (System.Linq.Expressions.MethodCallExpression m) [0x0006a] in <filename unknown>:0 
  at Realms.ExpressionVisitor.Visit (System.Linq.Expressions.Expression exp) [0x000ec] in <filename unknown>:0 
  at Realms.RealmResults`1[T].CreateResultsHandle () [0x00037] in <filename unknown>:0 
  at Realms.RealmResults`1[T].get_ResultsHandle () [0x0000d] in <filename unknown>:0 
  at Realms.RealmResults`1[T].GetEnumerator () [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[T]..ctor (IEnumerable`1 collection) <0x1001aa7e0 + 0x001df> in <filename unknown>:0 
  at System.Linq.Enumerable.ToList[TSource] (IEnumerable`1 source) <0x100659e70 + 0x0004b> in <filename unknown>:0 
  at MyNamespace.MyMethod (System.Collections.Generic.List`1 myListList) [0x000b0] in C:\PathToMyFile\MyFile.cs:140 }   System.NotSupportedException

I'm not sure what this means. Does it mean that my Where clause can only use a hardcoded string or int like the example below?

var results = realm.All<MyRealmType>().Where(x => x.Property == "stringToCompare");

If so this seems very limiting. Does anyone know how to resolve this.

Thanks in advance.


Solution

  • The answer provided by Will does work e.g. you have to copy the query term into a separate variable

    var queryTerm = otherVariable.Property;
    var results = realm.All<MyRealmType>().Where(x => x.Property == queryTerm);
    

    Maybe someone from Realm can explain why this is, and whether it will be fixed in the future. I suspect it has something to do with the Weaver. Just a guess.

    Thanks again Will