[英]How to compare SQLite datetime to C# string using Linq
I'm trying to Compare the SQLITE table value GameDate with a C# string, tempDate using a Linq statement. 我正在尝试将SQLITE表值GameDate与C#字符串,使用Linq语句的tempDate进行比较。 I'm getting an error on the "var list = db.Table()" line.
我在“ var list = db.Table()”行上遇到错误。
Since I can't get the Linq line to work. 由于我无法使Linq生产线正常工作。 I have just selected all the rows of the table and use a for loop to match.
我刚刚选择了表的所有行,并使用for循环进行匹配。 (it doesn't work either)
(也不起作用)
This is the Linq statement I'm trying to use. 这是我要使用的Linq语句。
// var list = db.Table().Where(n => n.GameDate== Convert.ToDateTime(tempDate)).ToList();
// var list = db.Table()。Where(n => n.GameDate == Convert.ToDateTime(tempDate))。ToList();
Here is my code: 这是我的代码:
int recCtr = 0;
var root = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "BaseBallOU.db");
List<string> myCollection = new List<string>();
foreach (Object obj in GameDateListBox.Items)
{
string tempDate= obj.ToString();
using (var db = new SQLite.SQLiteConnection(dbPath))
{
try
{
// var list = db.Table<Results>().Where(DateTime.Compare(GameDate.Value.Date, tempDate) == 0);
// var list = db.Table<Results>().Where(n => n.GameDate== Convert.ToDateTime(tempDate)).ToList();
var list = db.Table<Results>().ToList();
foreach (var item in list)
{
recCtr++;
if (item.GameDate.ToString("d") == tempDate.ToString())
{
myCollection.Add(item.GameDate.ToString());
}
}
}
catch { }
}
}
TIA, any help appreciated. TIA,任何帮助表示赞赏。
EF can't parse Convert.ToDateTime
to SQL. EF无法将
Convert.ToDateTime
解析为SQL。 Instead of that, you can declare DateTime
variable outside of the query. 取而代之的是,您可以在查询外部声明
DateTime
变量。
DateTime dt = Convert.ToDateTime(tempDate);
var list = db.Table().Where(n => n.GameDate == dt).ToList();
Also, you may need to compare only Date()
part of the DateTime
. 另外,您可能只需要比较
DateTime
Date()
部分。 Then you need to use on of canonical functions like EntityFunctions.TruncateTime()
: 然后,您需要使用诸如
EntityFunctions.TruncateTime()
类的规范函数:
DateTime dt = Convert.ToDateTime(tempDate);
var list = db.Table().Where(n => EntityFunctions.TruncateTime(n.GameDate) == dt.Date).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.