Realm .NET 使用 Contains() 的查询抛出 System.NotSupportedException 的地方

问题描述 投票:0回答:3

我正在使用 Realm for .NET v10.1.3,并且我有一个删除一些对象的方法。从表明支持 Contains 的文档中提取,我有以下片段:

var results = realm.All<DeviceEventEntity>()
    .Where(entity => ids.Contains(entity.Id));
                            
realm.RemoveRange(results);

但是当执行

realm.RemoveRange(results)
时,Realm会抛出System.NotSupportedException。我在这里做错了什么?或者Realm不支持Contains?

这是堆栈跟踪:

System.NotSupportedException
The method 'Contains' is not supported
   at Realms.RealmResultsVisitor.VisitMethodCall(MethodCallExpression node) in C:\jenkins\workspace\realm_realm-dotnet_PR-2362@2\Realm\Realm\Linq\RealmResultsVisitor.cs:line 378
   at Realms.RealmResultsVisitor.VisitMethodCall(MethodCallExpression node) in C:\jenkins\workspace\realm_realm-dotnet_PR-2362@2\Realm\Realm\Linq\RealmResultsVisitor.cs:line 164
   at Realms.RealmResults`1.CreateHandle() in C:\jenkins\workspace\realm_realm-dotnet_PR-2362@2\Realm\Realm\Linq\RealmResults.cs:line 65
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at Realms.RealmResults`1.get_ResultsHandle() in C:\jenkins\workspace\realm_realm-dotnet_PR-2362@2\Realm\Realm\Linq\RealmResults.cs:line 30
   at Realms.Realm.RemoveRange[T](IQueryable`1 range) in C:\jenkins\workspace\realm_realm-dotnet_PR-2362@2\Realm\Realm\Realm.cs:line 1279
   at DocLink.Client.Storage.Service.Event.DeviceEventService.<>c__DisplayClass2_0.<DeleteEvents>b__0() in

这是一个更完整的示例:

public Task DeleteEvents(List<ObjectId> ids) {
  return Task.Run(() => {
    using (var realm = GetRealm()) {
      using (var transaction = realm.BeginWrite()) {
        try {
          var results = realm.All<DeviceEventEntity>().Where(entity => ids.Contains(entity.Id));
          realm.RemoveRange(results);
          transaction.Commit();
        }
        catch (Exception exception) {
          transaction.Rollback();
          throw new ServiceException("Unable to delete events. Transaction has been rolled back.", exception);
        }
      }
    }
  });
}

此外,该库引用这样的文件似乎有点奇怪

C:\jenkins\workspace\realm_realm-dotnet_PR-2362@2\Realm\Realm\Linq\RealmResultsVisitor.cs
。这不是我系统上的任何内容,该库是通过 NuGet 引入的。

c# realm
3个回答
1
投票

文档说当你遇到NotSupportedException时需要使用

Filter
。阅读该方法的评论,获取
NSPredicate
备忘单的链接,您可以用它做很多事情:)


1
投票

更新问题。首先,感谢所有参与的人,并帮助我指明了正确的方向。最终的答案最终是一些事情的组合,但简而言之,正是这篇上一篇文章最终解决了问题。

当前版本的 Realm 已经支持 Mongo ObjectId,但是,在 Filter() 方法中使用 ObjectId 并没有真正起作用。因此,解决方案是最终使用字符串作为 PK,但在 DTO 中使用 ObjectId——在退出时转换为 ObjectId,并在进入 Realm 时转换为 ToString()。

public static class IQueryableExtensions {
    public static IQueryable<T> In<T>(this IQueryable<T> source, string propertyName, IList<ObjectId> objList)
        where T : RealmObject {
        var query = string.Join(" OR ", objList.Select(i => $"{propertyName} == '{i.ToString()}'"));
        var results = source.Filter(query);

        return results;
    }
}

我的代码使用扩展

public Task DeleteEvents(List<ObjectId> ids) {
        return Task.Run(() => {
            using (var realm = GetRealm())
            {
                using (var transaction = realm.BeginWrite())
                {
                    try {
                        // In order to support this with the current version of Realm we had to write an extension In()
                        // that explodes the list into a Filter() expression of OR comparisons. This also required us
                        // to use string as the underlying PK type instead of ObjectId. In this way, our domain object
                        // still expects ObjectId, so we ToString() on the way into realm and ObjectId.Parse() on the
                        // way out to our DTO.
                        var results = realm.All<DeviceEventEntity>().In("Id", ids);
                        realm.RemoveRange(results);
                        transaction.Commit();
                    }
                    catch (Exception exception)
                    {
                        transaction.Rollback();
                        throw new ServiceException("Unable to delete events. Transaction has been rolled back.", exception);
                    }
                }
            }
        });
    }

0
投票

此版本适用于 guid 属性

 public static IQueryable<T> In<T>(this IQueryable<T> source, string propertyName, IEnumerable<Guid?> objList)
        where T : IRealmObject
    {
        int count = 0;
        var query = string.Concat("Id in { $", string.Join(", $", objList.Select(i => count++)), "}");
        var results = source.Filter<T>(query, objList.Select(x=> (QueryArgument)x).ToArray());
        return results;
    }
© www.soinside.com 2019 - 2024. All rights reserved.