我正在尝试从 SQLite 中提取最新的 DateTime 字段,但它返回的时间不正确。
这是数据库中的数据:
这是我通过 LINQ 获取最新日期时间的方法:
public string getLastSyncTime()
{
using (var db = new SQLite.SQLiteConnection(this.DBPath))
{
var query = db.Table<SyncAudit>()
.OrderByDescending(c => c.SyncTime)
.Select(c => c.SyncTime)
.FirstOrDefault();
return query.ToString();
}
}
问题是它没有返回预期的日期时间。我收到 1/1/0001 凌晨 12 点:
我做错了什么?
编辑:这是正在使用的提供商,每个请求:https://components.xamarin.com/view/sqlite-net
编辑 2:请求的 SyncAudit 类:
class SyncAudit
{
public string Server { get; set; }
public string Database { get; set; }
public DateTime SyncTime { get; set; }
public int Successful { get; set; }
}
最佳答案
改变
public DateTime synctime{ get; set; }
到:
public DateTime? synctime { get; set; }
希望对您有所帮助。发生的情况是 DateTime 不是 NULL 数据类型,它强制在那里放置一个默认值 (01/01/0001) 以确保提交非空日期。不知道这是否是一个问题..尝试并检查
关于c# - LINQ to SQL 查询未返回正确的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344754/