You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unable to delete items using realm range, when using .Where(predicate) to retrieve results from realm.all
Below is the code used to fetch items and delete items in a range.
The realm.all<T> will return a valid Queryable however, with LINQWhere will return an IEnumerable so this causes the result entities cannot be passed as to RemoveRange where as per docs https://www.mongodb.com/docs/realm/sdk/dotnet/crud/delete/ it should be valid. If this is not valid, please update the docs. Thus, the conversion to AsQueryable()
Causing an exception
{System.ArgumentException: range should be the return value of .All or a LINQ query applied to it. (Parameter 'range')
.Where should not return an IEnumerable - if it does, it means you're using the overload that takes a Func<T, bool> rather than Expression<Func<T, bool>>. By using the Func overload, you're executing the filter in-memory rather than using the query engine, which is also why RemoveRange complains - calling .AsQueryable will not convert an in-memory collection into a database query.
If you can use an expression and create a database query, you should be able to use the optimized RemoveRange method. If that's not possible, you should materialize the collection and iterate over it, removing the items individually:
.Where should not return an IEnumerable - if it does, it means you're using the overload that takes a Func<T, bool> rather than Expression<Func<T, bool>>. By using the Func overload, you're executing the filter in-memory rather than using the query engine, which is also why RemoveRange complains - calling .AsQueryable will not convert an in-memory collection into a database query.
If you can use an expression and create a database query, you should be able to use the optimized RemoveRange method. If that's not possible, you should materialize the collection and iterate over it, removing the items individually:
Well, this is a confusing manner, I think the docs can be updated to specify that the clause should take Expression overload than Func<T, bool. Thanks for clarifying the issue.
What happened?
Unable to delete items using realm range, when using
.Where(predicate)
to retrieve results fromrealm.all
Below is the code used to fetch items and delete items in a range.
The
realm.all<T>
will return a validQueryable
however, withLINQ
Where
will return anIEnumerable
so this causes the resultentities
cannot be passed as toRemoveRange
where as per docs https://www.mongodb.com/docs/realm/sdk/dotnet/crud/delete/ it should be valid. If this is not valid, please update the docs. Thus, the conversion toAsQueryable()
Causing an exception
As per checking the resource https://github.com/realm/realm-dotnet/blob/main/Realm/Realm/Realm.cs
Ensures that the type is realm results -- which is an
IQueryable
argument.Removing the
Where
clause ensures that the range is valid, thus not causing anyExceptions
Repro steps
Version
10.19.0
What Atlas Services are you using?
Local Database only
What type of application is this?
Xamarin
Client OS and version
Android 12.0
Code snippets
No response
Stacktrace of the exception/crash you're getting
No response
Relevant log output
No response
The text was updated successfully, but these errors were encountered: