Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to delete items using realm.RemoveRange #3486

Closed
jbtdevgit opened this issue Dec 3, 2023 · 2 comments
Closed

Unable to delete items using realm.RemoveRange #3486

jbtdevgit opened this issue Dec 3, 2023 · 2 comments

Comments

@jbtdevgit
Copy link

jbtdevgit commented Dec 3, 2023

What happened?

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.

IEnumerable<T> entities = realm.All<T>().Where(predicate);
realm.RemoveRange(entities.AsQueryable());

The realm.all<T> will return a valid Queryable however, with LINQ Where 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')

As per checking the resource https://github.com/realm/realm-dotnet/blob/main/Realm/Realm/Realm.cs

var results = Argument.EnsureType<RealmResults<T>>(range, "range should be the return value of .All or a LINQ query applied to it.", nameof(range));

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 any Exceptions

Repro steps

  • Delete item using range

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

@nirinchev
Copy link
Member

.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:

var query = realm.All<T>().Where(predicate).ToArray();
foreach (var item in query)
{
    realm.Remove(item);
}

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2023
@jbtdevgit
Copy link
Author

.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:

var query = realm.All<T>().Where(predicate).ToArray();
foreach (var item in query)
{
    realm.Remove(item);
}

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants