【发布时间】:2021-05-13 03:52:45
【问题描述】:
我正在使用 Realm for .NET v10.1.3,并且我有一个删除一些对象的方法。从表明支持 Contains 的文档中提取,我有以下 sn-p:
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 拉入的。
【问题讨论】:
-
Or does Realm not support Contains?正确。 google.com/search?q=realm+linq+contains+support -
这有点令人沮丧,因为 Mongo 在他们的在线文档中专门展示了这个示例:Debug.WriteLine("Ali 或 Jamie 的任务:" + tasks.Where(t => new List { "Ali", "Jamie" }.Contains(t.Assignee))); Filter On Object Properties
-
@TravisBeech 的那些文档在哪里?
-
请分享minimal reproducible example,包括
ids的填充方式。