C# 使用LINQ获取最新的datetime列
我有一个镜像SQLite表的简单类C# 使用LINQ获取最新的datetime列,c#,linq,C#,Linq,我有一个镜像SQLite表的简单类 class SyncAudit { public string Server { get; set; } public string Database { get; set; } public string SyncTime { get; set; } public int Successful { get; set; } } 我试图构造一个LINQ方法,返回最近的同步时间 public string getLastS
class SyncAudit
{
public string Server { get; set; }
public string Database { get; set; }
public string SyncTime { get; set; }
public int Successful { get; set; }
}
我试图构造一个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();
}
}
Using:
SyncTime
在您的模型中是一个字符串,但似乎(或应该)是一个DateTime
。所以在数据库中修复它。如果不可能,您必须解析它:
DateTime lastSyncTime = db.Table<SyncAudit>().AsEnumerable() // Do the rest of the processing in memory
.Select(c => DateTime.Parse(c.SyncTime))
.OrderByDescending(dt => dt)
.FirstOrDefault();
return lastSyncTime;
DateTime lastSyncTime=db.Table().AsEnumerable()//在内存中执行其余的处理
.Select(c=>DateTime.Parse(c.SyncTime))
.OrderByDescending(dt=>dt)
.FirstOrDefault();
返回上次同步时间;
SyncTime
在您的模型中是一个字符串,但似乎(或应该)是一个DateTime
。所以在数据库中修复它。如果不可能,您必须解析它:
DateTime lastSyncTime = db.Table<SyncAudit>().AsEnumerable() // Do the rest of the processing in memory
.Select(c => DateTime.Parse(c.SyncTime))
.OrderByDescending(dt => dt)
.FirstOrDefault();
return lastSyncTime;
DateTime lastSyncTime=db.Table().AsEnumerable()//在内存中执行其余的处理
.Select(c=>DateTime.Parse(c.SyncTime))
.OrderByDescending(dt=>dt)
.FirstOrDefault();
返回上次同步时间;
这看起来不错,有什么问题吗?SyncTime
真的是数据库中的string
吗,还是应该是DateTime
?显示代码时不会出现MissingMethodException
。这是我遇到的错误。但是,将模型更改为DateTime可以修复它。这看起来不错,有什么问题吗?SyncTime
真的是数据库中的string
吗,还是应该是DateTime
?显示代码时不会出现MissingMethodException
。这是我遇到的错误。但是,将模型更改为DateTime可以修复它。