realm / realm-dotnet

Realm is a mobile database: a replacement for SQLite & ORMs
https://realm.io
Apache License 2.0
1.24k stars 162 forks source link

Query conditions on linked properties #806

Open kristiandupont opened 8 years ago

kristiandupont commented 8 years ago

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

kolyayablochckin commented 7 years ago

That would be really great

Shaddix commented 7 years ago

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 commented 7 years ago

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)

charlesroddie commented 4 years ago

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...
nirinchev commented 4 years ago

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.

JamieColclough commented 3 years ago

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

nirinchev commented 3 years ago

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

peppy commented 2 years ago

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.

Nerves82 commented 1 year ago

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.

nirinchev commented 1 year ago

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.