[英]How to query for the oldest object from db4o?
I have objects that has a DateTime property, how can i query for the oldest object? 我的对象具有DateTime属性,如何查询最早的对象?
After asking on db4o forum, I get the answer: 在db4o论坛上询问后,我得到了答案:
It's quite easy: create a sorted SODA-Query and
take the first / last object from the resulting ObjectSet
. 这很容易:创建一个排序的
SODA-Query and
从结果ObjectSet
获取第一个/最后一个对象。 Don't iterate the ObjectSet
(therefore the objects won't be activated), just take the required object directly via #ObjectSet.Get(index)
. 不要迭代
ObjectSet
(因此将不会激活对象),只需直接通过#ObjectSet.Get(index)
获取所需的对象。
Please note: db4o supports just a limited set of performant sortings (alphabetical, numbers, object ids) in query execution, so maybe you have to store your DateTime as milliseconds to achieve good performance. 请注意:db4o在查询执行中仅支持一组有限的性能排序(字母,数字,对象ID),因此也许您必须将DateTime存储为毫秒才能获得良好的性能。
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, eg 现在,您可以使用Linq,SODA或本机查询以任意方式查询对象。
IObjectContainer container = ...;
Customer oldestCustomer = container.Query<Customer>().OrderBy(p => p.DateSignedUp).First();
However, there is a set of pitfalls: 但是,有一些陷阱:
DateTime
in your persisted object. DateTime
。 I have had massive problems with them. long
instead and copy the ticks from the respective DateTime
. long
并复制相应DateTime
的刻度。 Store all times in UTC, unless you're explicitly referring to local time, such as in the case of bus schedules. where
constrain, because that will be fast. where
施加约束,因为这样做会很快。 See also my blogpost regarding that performance issue, which can become very annoying already at ~50-100k objects. Best, 最好,
Chris 克里斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.