question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Queries: order by property of related entity

See original GitHub issue

is possible to sort parent items by child property ?

for example :

Author

  • name
  • birthday

Birthday

  • date

i want list all authors, but when birthday != null must be sorting first

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
greenrobotcommented, Aug 12, 2017

@smith6k If you are asking about the equivalent of a JOIN: yes this is planned soon-ish.

2reactions
RobbWatershedcommented, Sep 22, 2020

For those who are interested, here’s the workaround I finally came up with. It doesn’t implement the ability of LiveData to update datasets as they change, but it works correctly with PagedList, which is an okay tradeoff in my very own case.


Goal

Produce a LiveData<PagedList<Content>> ordered by GroupItem.order, where Content has a N…1 relationship with GroupItem.

In ObjectBox terms, the data model is as follows :

  • GroupItem entity has public ToOne<Content> content;
  • Content entity has @Backlink(to = "content") public ToMany<GroupItem> groupItems;

Vanilla way of implementing (does not work when ordering with GroupItem.order for the reasons stated in this issue)

  1. Produce the adequate Query<Content>
  2. Get what you need by calling LivePagedListBuilder<>(new ObjectBoxDataSource.Factory<>(Query<Content>), cfg).build()

Workaround

  1. Produce a Query<GroupItem>
    • use query.order(GroupItem_.order)
    • use query.link(GroupItem_.content) to apply whatever filter should be applied to Content
  2. Fetch all Content ID’s with Stream.of(query.build().find()).map(gi -> gi.content.getTargetId()).toList()
  3. Use an ObjectBoxPredeterminedDataSource (custom class; see code below) that will produce your LiveData<PagedList<Content>>.

This DataSource is fed with :

  • the pre-determined list of ID’s produced in step 2
  • a fetcher function Function<List<Long>, List<I>> that will be used to fetch your objects in the DB. In my case, the fetcher is a good old store.boxFor(Content.class).get(idList)

My implementation of ObjectBoxPredeterminedDataSource is inspired by ObjectBoxDataSource : https://gist.github.com/RobbWatershed/d6360f797d33e63606f2902b7621bf6c

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to order by with related entity properties in EF core
u want order by Locality ASC,right? I think Class type of query is IEnumerable,so you can use lumbda expression.
Read more >
Loading Related Entities - EF6 - Microsoft Learn
Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query.
Read more >
JS | Query Examples - Breeze
Single property sorting; Multiple property sorting; Related property sorting. Paging with 'skip' and 'take' ... Query by key; Query a bag of entities...
Read more >
[Solved]-Sort entities based on a substring-Entity Framework
Related Query · Sort entities based on a substring · LINQ to Entities Sort Expression based on client input · How to query...
Read more >
Entity queries | Apple Developer Documentation
Help the system find the entities your app defines and use them to resolve ... Details about a specific property you use to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found