realm-dotnet
realm-dotnet copied to clipboard
Query conditions on linked properties
So you can filter by, say, r.All<Dog>().Where(dog => dog.Owner.FirstName == "John")
That would be really great
is there any ETA for that? It seems that all other platforms already have that feature. May be some kind of simpler implementation via strings instead of LINQ could be possible?
Any design so we could help with an implementation?
That's what we are actually using since mid-Feb: https://github.com/Shaddix/realm-dotnet
The syntax (as well as implementation, actually) was ported from realm-java: https://github.com/Shaddix/realm-dotnet/blob/master/Tests/Tests.Shared/LinkQueryTests.cs
It's currently looks like a hack, rather than proper implementation, but it's much better than living without link queries. Here's the usage example:
realm.All<Owner>().AddLinkQuery(x => x.TopDog.Color, PredicateOperator.Equal, "Black")
I do understand, that proper implementation via LINQ is much more complex (i.e. will not be available coming months), so the solution like that could be helpful (at least for those who are eager for LinkQuery functionality)
I tried to workaround this with
r.All<Dog>().Where(dog =>
r.All<Person>().Any(person =>
person.FirstName == "John" && person == dog.Owner))
but get the error
Exception thrown: 'System.Exception' in Realm.dll
An unhandled exception of type 'System.Exception' occurred in Realm.dll
We already have a table...
This is not supported - there's no way to express the condition as a query that will get executed by the database. Instead, you can use the IQueryable<T>.Filter
extension method. For your case, I believe the syntax should look like:
var dogs = r.All<Dog>().Filter("Owner.FirstName == 'John'");
You can read up more on the syntax here: https://realm.io/docs/javascript/latest/api/tutorial-query-language.html. This page is covering the JS SDK, so you won't be able to just copy-paste the examples in a .NET app, but the syntax itself is identical. The .NET docs are being updated and we hope to have that functionality covered soon.
Any update on when there would be an update that fixes this and gives us a solution that the OP asked for?
We've put some work toward that, but we don't have a timeline for when that will be complete and released.
This has come up multiple times in our project in a way that degrades code legibility by having to use the raw Filter
method (coming from EF where we were passing queryable Expression
s to a manager class to perform the actual queries). Can work around it, but it's not great.
Is there any update on this? It is really hard to believe that this is not a thing already. It's been so long since this issue was opened.
I agree - it's not ideal, but we haven't been able to prioritize it higher because the team has been focused on more important features for which there is no workaround. While using raw string queries is not as nice as using LINQ, it's at least an option and if LINQ support is absolutely critical, you could write an expression visitor that converts a LINQ expression into an RQL string and pass that to Realm. If this turns into a well designed and tested project, we'd be happy to accept it as a contribution to the Realm SDK.