realm.xamarin throws error: The method 'Contains' is not supported. same for 'Any'

送分小仙女□ 提交于 2019-12-05 11:05:47

OK, that was a lot of fun. The answer (for right now) is to build a LINQ Expression Tree.

Realm supports LINQ - we parse the Expression Tree (OK, some bits are missing but the basics are there and we're improving regularly - we will add Contains).

So, any time you need to build up a completely arbitrary query expression at runtime you can do so with the power of LINQ.

The example below may appear a bit complicated but the point is that it uses a standard Expression tree so you can use any other code which builds such trees.

class HasIntKey : RealmObject {
    public int UserKey { get; set; }
}


private void FindingManyMatches()
{
    using (var theRealm = Realm.GetInstance ("TestingManyKeys.realm")) {
        // create some sample data
        theRealm.Write (() => {
            for (int i = 1; i < 10000; i++) {
                var obj = theRealm.CreateObject<HasIntKey> ();
                obj.UserKey = i;
            }
        });
        var allInts = theRealm.All<HasIntKey>();
        Console.WriteLine ($"Created {allInts.Count()} objects");

        var idsToMatch = new[] {42,1003,400,57, 6009};

        // technique for how to search for many matches
        // use Expression Trees to dynamically build the LINQ statement
        // see https://msdn.microsoft.com/en-us/library/mt654267.aspx
        ParameterExpression pe = Expression.Parameter (typeof(HasIntKey), "p");

        // building an expression like:
        // allInts.Where (p => p.UserKey == idsToMatch[0] || p.UserKey == idsToMatch[1]...);
        Expression chainedByOr = null;
        // left side of the == will be the same for each clause we add in the loop
        Expression left = Expression.Property(pe, typeof(HasIntKey).GetProperty("UserKey"));  
        foreach (int anId in idsToMatch) {
            // Create an expression tree that represents the expression 'p.UserKey == idsToMatch[n]'.
            Expression right = Expression.Constant(anId);
            Expression anotherEqual = Expression.Equal(left, right);
            if (chainedByOr == null)
                chainedByOr = anotherEqual;
            else 
                chainedByOr = Expression.OrElse (chainedByOr, anotherEqual);
        }
        MethodCallExpression whereCallExpression = Expression.Call(
            typeof(Queryable),
            "Where",
            new Type[] { allInts.ElementType },
            allInts.Expression,
            Expression.Lambda<Func<HasIntKey, bool>>(chainedByOr, new ParameterExpression[] { pe }));

        // Create an executable query from the expression tree.
        IQueryable<HasIntKey> results = allInts.Provider.CreateQuery<HasIntKey>(whereCallExpression);

        // now we have our results, display some details!
        Console.WriteLine ($"Found {results.Count()} objects");

        // Enumerate the results.
        foreach (HasIntKey anObj in results)
            Console.WriteLine($"Found key {anObj.UserKey}");
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!