WP7 Sterling Database DateTime Index

时间:2011-08-17 05:41:23

标签: c# windows-phone-7 sterling-db

我正在使用Sterling Database并定义了一个类和索引,如下所示

public class SingleEventDB :TimetableEventDB
{
    public DateTime EventDateTime { get; set; }
}

CreateTableDefinition<SingleEventDB, string>(k => k.UniqueId)
    .WithIndex<SingleEventDB,DateTime,string>("BYDATETIME",i=>i.EventDateTime)

我像这样访问

public List<SingleEvent> GetAllSingleEvents(DateTime StartDate)
{
    var allSingleEvents = new List<SingleEvent>();

    var result =
        from eveItemDB in App.MyDatabase.Query<SingleEventDB, DateTime, string>("BYDATETIME")
        where (eveItemDB.Index >= StartDate)
        select new SingleEvent
        {
            UniqueId = eveItemDB.LazyValue.Value.UniqueId,
            NextDateTime = eveItemDB.Index,
            Details = eveItemDB.LazyValue.Value.Details,
            Location = eveItemDB.LazyValue.Value.Location
        };

    foreach (SingleEvent eveItem in result)
    {
        allSingleEvents.Add(eveItem);

    }

    return allSingleEvents;
}

然而,当where子句正确地过滤掉早于输入日期的对象时,它们将按照创建它们的顺序返回,而不是以索引(DateTime)顺序返回。

这是预期的,还是我做错了什么。

有没有办法在t中插入orderby子句。

1 个答案:

答案 0 :(得分:1)

好像你错过了Enumerable.ToList扩展方法了!是的,您只需在代码中添加orderby eveItemDB.Index,就像这样:

public List<SingleEvent> GetAllSingleEvents(DateTime StartDate)
{
    var result =
        from eveItemDB in App.MyDatabase.Query<SingleEventDB, DateTime, string>("BYDATETIME")
        where (eveItemDB.Index >= StartDate)
        orderby eveItemDB.Index
        select new SingleEvent
        {
            UniqueId = eveItemDB.LazyValue.Value.UniqueId,
            NextDateTime = eveItemDB.Index,
            Details = eveItemDB.LazyValue.Value.Details,
            Location = eveItemDB.LazyValue.Value.Location
        };

    return result.ToList();
}