7

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.

rideintothesun
  • 1,628
  • 2
  • 12
  • 29
  • WAG, but try `var foo = otherVariable.Property;` then use `foo` in the query. –  May 25 '16 at 12:48

3 Answers3

6

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

rideintothesun
  • 1,628
  • 2
  • 12
  • 29
  • 1
    The reason for this is that our LINQ provider doesn't yet recursively trace through property access. This is an obvious and significant limitation so we will fix it and it has quite high priority. Thank you for the feedback and for posting the workaround for now! – Kristian Dupont May 26 '16 at 07:37
  • @KristianDupont I dont see this issue listed in the Github Issues, Maybe I'm just not good a searching for it. Can you please add a Link to the issues so that it can be followed, Thanks – J3RM Jun 24 '16 at 20:34
  • @KristianDupont Hi, I'm not sure if you're still working on the Realm project. But I encountered this issue again in 2021. Is there any progress on the LINQ provider? – John Cido Oct 23 '21 at 18:42
  • @JohnCido sorry, I am no longer involved. But I am sure that if you create a ticket on https://github.com/realm/realm-dotnet the good people will help you :-) – Kristian Dupont Oct 24 '21 at 14:40
  • @KristianDupont Thanks for the update! I'll check the repo then. <3 – John Cido Oct 25 '21 at 15:27
1

Try this (it works for me):

        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();
Andrey
  • 21
  • 1
0

I did like this:

var results = realm.All<MyRealmType>().AsEnumerable().Where(x => x.Property == otherVariable.Property);
Vladimir Pankov
  • 377
  • 3
  • 8