Frage

Ich habe Objekte mit einer DateTime -Eigenschaft. Wie kann ich nach dem ältesten Objekt abfragen?

Nachdem ich im DB4O -Forum gefragt habe, bekomme ich die Antwort:

Es ist ziemlich einfach: Erstellen Sie eine sortierte SODA-Query and Nehmen Sie das erste / letzte Objekt aus dem resultierenden ObjectSet. Iterieren Sie nicht ObjectSet (Daher werden die Objekte nicht aktiviert.) Nehmen Sie einfach das erforderliche Objekt direkt über #ObjectSet.Get(index).

Bitte beachten Sie: DB4O unterstützt nur eine begrenzte Reihe von Performanten (alphabetischen, Zahlen, Objekt -IDs) in der Abfrageausführung. Vielleicht müssen Sie Ihre DateTime als Millisekunden speichern, um eine gute Leistung zu erzielen.

War es hilfreich?

Lösung

first of all, your object needs to keep track of the time itself, so it depends on your requirements:

class Customer
{
    public DateTime DateSignedUp {get; private set;}
    // ...
}

Now, you can query for the object in whatever way you like, using Linq, SODA, or Native Queries, e.g.

IObjectContainer container = ...;
Customer oldestCustomer = container.Query<Customer>().OrderBy(p => p.DateSignedUp).First();

However, there is a set of pitfalls:

  1. Don't use DateTime in your persisted object. I have had massive problems with them. I can't reproduce the problem so I couldn't report it yet, but I can personally not recommend using them. Use a long instead and copy the ticks from the respective DateTime. Store all times in UTC, unless you're explicitly referring to local time, such as in the case of bus schedules.
  2. Put an index on the time
  3. The order operation could be very, very slow for large amounts of objects because of issue COR-1133. If you have a large amount of objects and you know the approximate age of the object, try to impose a where constrain, because that will be fast. See also my blogpost regarding that performance issue, which can become very annoying already at ~50-100k objects.

Best,
Chris

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top