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.

Ordering by a property that belongs to a base class

See original GitHub issue

Dear Mauricio,

First, thank you very much for your wonderful project. I’m using LiteDB 4.1 and encountered a problem when running an order by query.

I have a class Rect that derives from a class Shape, and I’m inserting Rect instances to litedb. Now, I want to select all the rects from the db ordered by a property (“LastUpdated”) that exists in the parent Shape class and it doesn’t work (I get the items in random order…)

internal static List<T> AllEntriesOrderedByDescending<T>(this LiteCollection<T> collection, int limit)
{
    return collection.Find(Query.All("LastUpdated", Query.Descending), skip: 0, limit: limit).ToList();
}

Nothing is indexed and the property of LastUpdated is fetched correctly in a regular “select all” query. I’ll be happy for your help.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mbdavidcommented, Dec 31, 2017

Hi @tabtile, thanks!! Did you have an index in LastUpdated column? Use query order must be over an index only (not over a field only).

Try do:

collection.EnsureIndex(x => x.LastUpdated);

Before run your Find

Another way to sort is use FindSort method, but it’s avaiable only in LiteEngine instance: You can use like this:

var docs = db.Engine.FindSort("mycol", Query.All(), "$.LastUpdated", Query.Descending);
    
var entities = docs.Select(x => x.Mapper.ToObject<T>(x)).ToList();
1reaction
tabtilecommented, Jan 1, 2018

Hey Mauricio,

You’ve just made my day 😃 It works! Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sort the properties based on base classes and attribute order
You can do this by crawling the list of BaseType s on DemoClass , inspecting the DeclaringType property of your PropertyInfo s, and...
Read more >
baseClass
The property searchPath identifies objects. This property is usually constructed from the name and class of the object, and the names and classes...
Read more >
Inheritance in C# and .NET
While all other members of a base class are inherited by derived classes, whether they are visible or not depends on their accessibility....
Read more >
Multiple Base Classes
The order in which base classes are specified isn't significant except in certain cases where constructors and destructors are invoked.
Read more >
TypeScript: Documentation - Classes
The order of class initialization, as defined by JavaScript, is: The base class fields are initialized; The base class constructor runs; The derived...
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