realm-dotnet: LINQ Query Using Related Objects Not Working?

Goal

Need to be able to query using LINQ with related objects. e.g.

var employee = realm.All<Employee>().SingleOrDefault( e => e.Department.UniqueId == fooId && e.Name == fooName );

Expected Results

Should return the Employee with the name from the specified Department.

Actual Results

Always returns no matches. Where() also returns no matches. However, eliminating the e.Department and searching only on employee name works fine but obviously does not scope to Department as intended.


public class Department : RealmObject
{
  [Primary Key]
  public string UniqueId { get; set; }
}

public class Employee : RealmObject
{
  [Primary Key]
  public string Name { get; set; }

  // Parent
  public Department Department { get; set; }
}

Version of Realm and tooling

Realm version(s): ? 0.80

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Would love to see extended linq support!

By the way, if you go for your own LINQ provider and it turns out great, we’d be more than happy to accept a PR 😊

We’re in the process of overhauling our docs which is why the current pages have been a bit stale. But we’ll definitely be adding that when the new docs launch.

OMG how did I miss this. Could you please add this to the .NET documentation? https://realm.io/docs/dotnet/latest/

I should have visually scanned the API/intellisense - I thought it was weird that the other languages could filter on a string but .net couldnt… just missing from the docs page I guess. My searches just kept taking me back to this topic where the predicate filtering is never mentioned as an alternative.

Thanks!

Now I can write my own LINQ provider that adds all the missing predicate capabilities too… and the skies open!!

You can use the .Filter method on IQueryable - it accepts a string predicate just like Studio.

We definitely would like to improve our query support and it’s fairly high on our TODO list. Unfortunately, we don’t have a timeline for when it’ll land at the moment.

That being said, have you benchmarked your data model to figure out when using LINQ to objects will become a bottleneck? The thousands of items figure I mentioned is not a threshold by any means and instead of taking it at face value, it’s best to do some profiling to figure out when it becomes a problem for your app and if there are obvious workarounds.

For example, it’s unlikely you need thousands of items displayed on the screen. You can do something like: realm.All<Foo>().Where(some-supported-filter).AsEnumerable().Where(unsupported-filter).Take(100).ToArray() - this way, unless you hit a pathological case, you should get results fairly quickly. Then you can continue paginating: I highly doubt some user will scroll through thousands of records.