为什么使用SQLITE where子句无效



我正在尝试创建这样的SQLITE查询(第一种方法):

        int count;
        using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
            {
            count = ( from p in db.Table<TickRecord>()
                      where (p.TickStartDate.LocalDateTime >= start && p.TickEndtDate.LocalDateTime <= end )
                      select (int)p.DurationInSeconds ).Sum();
            }
        return count;

运行查询时,应用程序在where子句上崩溃。

我能够做到这一点(第二种方法):

        ObservableCollection<TickRecord> records;
        // Create a new connection
        using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
           {
           records = new ObservableCollection<TickRecord>( db.Table<TickRecord>().Select( i => i ) );
           }
        int count = records.Where( record => record.TickStartDate.LocalDateTime >= start && record.TickEndDate.LocalDateTime <= end ).Sum( record => record.DurationInSeconds );

有没有一种方法可以使用我的第一种方法实现同样的效果?

Thx

您不应该使用成员访问权限'。查询中的LocalDateTime'。Linq处理器无法转换"。LocalDateTime"转换为sqlite查询,很简单,因为sqlite中没有等效的函数。

结果是在你难过的时候抛出了一个异常:

[…]成员访问失败[…]。

如果您需要'的功能。LocalDateTime',那么您应该尝试从数据库中获取表的所有条目,并在以后使用where查询(如果您已经收到所有数据)。

int count;
using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
{
    var results = db.Table<TickRecord>().ToList();
    count = ( from p in results
              where (p.TickStartDate.LocalDateTime >= start && p.TickEndtDate.LocalDateTime <= end )
              select (int)p.DurationInSeconds ).Sum();
}
return count;

根据whymatter:修改代码

        int count;
        using ( var db = new SQLiteConnection( new SQLitePlatformWinRT(), DbPath ) )
            {
            count = ( from p in db.Table<TickRecord>()
                      where ( p.TickStartDate >= start && p.TickEndDate <= end )
                      select (int)p.DurationInSeconds ).Sum();
            }
        return count;

Thx!

最新更新