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

Query conditions on linked properties #806

Open
kristiandupont opened this issue Aug 29, 2016 · 10 comments
Open

Query conditions on linked properties #806

kristiandupont opened this issue Aug 29, 2016 · 10 comments

Comments

@kristiandupont
Copy link
Contributor

kristiandupont commented Aug 29, 2016

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

@kolyayablochckin
Copy link

That would be really great

@Shaddix
Copy link

Shaddix commented Feb 9, 2017

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?

@nirinchev nirinchev self-assigned this Mar 22, 2017
@Shaddix
Copy link

Shaddix commented May 26, 2017

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
Copy link

charlesroddie commented Sep 15, 2020

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
Copy link
Member

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
Copy link

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

@nirinchev
Copy link
Member

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

@peppy
Copy link

peppy commented Jan 7, 2022

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
Copy link

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants