Realm dotnet - rhs of the binary Equal operator must be a constant or closure expression

Hi i just started using realm dotnet

When I execute a simple query like

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

So, in the Where clause, I compare two strings to get the data I need from the area.

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 that means. Does this mean that my Where clause can only use hardcoded string or int, as in the example below?

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

If so, this seems very limited. Does anyone know how to solve this.

Thanks in advance.

+4
source share
2 answers

The answer provided by Will works, for example. you need to copy the query term into a separate variable

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

, - , , . , - . .

+4

( ):

        System.Func<YourItem, bool> predicate = (YourItem item) =>
        {
            return !item.BoolProperty && item.ParentID == parent?.ID;
        };
        return Realms.Realm.GetInstance().All<YourItem>().Where(predicate).OrderBy(item => item.Position).ToList();
-1

All Articles