realm-dotnet icon indicating copy to clipboard operation
realm-dotnet copied to clipboard

Query conditions on linked properties

Open kristiandupont opened this issue 8 years ago • 10 comments

So you can filter by, say, r.All<Dog>().Where(dog => dog.Owner.FirstName == "John")

kristiandupont avatar Aug 29 '16 13:08 kristiandupont

That would be really great

kolyayablochckin avatar Oct 12 '16 05:10 kolyayablochckin

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?

Shaddix avatar Feb 09 '17 15:02 Shaddix

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)

Shaddix avatar May 26 '17 10:05 Shaddix

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

charlesroddie avatar Sep 15 '20 14:09 charlesroddie

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.

nirinchev avatar Sep 15 '20 19:09 nirinchev

Any update on when there would be an update that fixes this and gives us a solution that the OP asked for?

JamieColclough avatar Sep 02 '21 09:09 JamieColclough

We've put some work toward that, but we don't have a timeline for when that will be complete and released.

nirinchev avatar Sep 02 '21 10:09 nirinchev

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 Expressions to a manager class to perform the actual queries). Can work around it, but it's not great.

peppy avatar Jan 07 '22 14:01 peppy

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.

Nerves82 avatar Aug 28 '23 18:08 Nerves82

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.

nirinchev avatar Aug 28 '23 19:08 nirinchev