2016-04-29 56 views
0

我想創建一個SQLite這樣的查詢(第一種方法):爲什麼使用SQLITE where子句不起作用?

 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

+3

你收到了什麼異常? – Michael

+1

拋出的異常:SQLite.Net.dll中的'System.NotSupportedException' 成員訪問無法編譯表達式 –

回答

1

,則不應使用成員訪問'.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; 
0

修改每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!