[英]How to use a DateTime in a where clause in Sqlite.Net
我正在编写一个使用Sqlite.Net
的移动应用程序,我正在尝试使用DateTime
属性和LINQ to SQL过滤Table
我有以下代码:
var validDates = Database.Connection.Table<Dates>()
.Where(x => x.StartDate <= DateTime.Today && x.EndDate >= DateTime.Today)
.ToList();
但这会引发未将对象引用设置为对象实例的情况,而Sqlite.Net
似乎表明该错误在Sqlite.Net
库中,这使我相信我没有正确使用DateTimes
。
警告:未处理的异常:System.Reflection.TargetInvocationException:调用的目标已引发异常。 ---> System.NullReferenceException:对象引用未设置为对象的实例。 09-28 13:01:24.293 W / Xamarin.Insights(31238):在SQLite.Net.TableQuery
1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 [T] queryArgs)[0x00613]在<8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W / Xamarin.Insights(31238):在SQLite.Net.TableQuery1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
<8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W / Xamarin.Insights(31238)中的1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 [T] queryArgs)[0x0064b]:在SQLite.Net.TableQuery1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
<8f2bb39aeff94a30a8628064be9c7efe>:0:0-09-28 13:01:24.293 W / Xamarin.Insights(31238)中的CompileExpr1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 [T] queryArgs)[0x00027]:在SQLite.Net.TableQuery1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 [T] queryArgs)[0x00027]在<8f2bb39aeff94a30a862806464be9c7efe>:0 09-28 13:01 :24.293 W / Xamarin.Insights(31238):在SQLite.Net.TableQuery1[T].GenerateCommand (System.String selectionList) [0x0006d] in <8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W/Xamarin.Insights(31238): at SQLite.Net.TableQuery
1[T].GenerateCommand (System.String selectionList) [0x0006d] in <8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W/Xamarin.Insights(31238): at SQLite.Net.TableQuery
1 [T] .GetEnumerator()[0x00008]在<8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W / Xamarin.Insights(31238):at System.Collections.Generic.List1[T]..ctor (System.Collections.Generic.IEnumerable
1 [T]集合)[0x00073]在/Users/builder/data/lanes/3819/c1d1c79c/source/mono/mcs/class/referencesource/mscorlib/system/collections/generic/list.cs:98 09-28 13:01:24.293 W / Xamarin.Insights(31238):位于System.Linq.Enumerable.ToList [TSource](System.Collections.Generic.IEnumerable`1 [T]源)[0x00011]在/ Users / builder / data / lanes / 3819 / c1d1c79c /源/单声道/ MCS /类/ referencesource / System.Core程序/系统/ LINQ的/ Enumerable.cs:861
将其作为sql statemnet进行,如下所示:
var query = string.Format("Select * from [Dates.Dates] where StartDate<=date('{0:yyyy-MM-dd}') and EndDate>=date('{1:yyyy-MM-dd}')", DateTime.Today, DateTime.Today);
但是有没有办法使用LINQ to SQL进行DateTime
查询呢?
额外信息
我的StoreDateTimeAsTicks
设置设置为True
我对StartDate
和EndDate
定义都是DateTimes
我的Dates
类如下所示:
[Table("Dates.Dates")]
public class Dates : BaseModel
{
public Dates()
{
//Don't fire notifications by default, since
//they make editing the properties difficult.
this.NotifyIfPropertiesChange = false;
}
[PrimaryKey]
[NotNull]
[AutoIncrement, Column("Id")]
public int Id
{
get { return Id_private; }
set { SetProperty(Id_private, value, (val) => { Id_private = val; }, Id_PropertyName); }
}
public static string Id_PropertyName = "Id";
private int Id_private;
[NotNull]
[Column("Name")]
public string Name
{
get { return Name_private; }
set { SetProperty(Name_private, value, (val) => { Name_private = val; }, Name_PropertyName); }
}
public static string Name_PropertyName = "Name";
private string Name_private;
[NotNull]
[Column("StartDate")]
// The actual column definition, as seen in SQLite
public string StartDate_raw { get; set; }
public static string StartDate_PropertyName = "StartDate";
// A helper definition that will not be saved to SQLite directly.
// This property reads and writes to the _raw property.
[Ignore]
public DateTime StartDate {
// Watch out for time zones, as they are not encoded into
// the database. Here, I make no assumptions about time
// zones.
get { return StartDate_raw != null ? DateTime.Parse(StartDate_raw) : StartDate = DateTime.Now; }
set { SetProperty(StartDate_raw, StartDate_ConvertToString(value), (val) => { StartDate_raw = val; }, StartDate_PropertyName); }
}
// This static method is helpful when you need to query
// on the raw value.
public static string StartDate_ConvertToString(DateTime date)
{
return date.ToString("yyyy-MM-dd");
}
[NotNull]
[Column("EndDate")]
// The actual column definition, as seen in SQLite
public string EndDate_raw { get; set; }
public static string EndDate_PropertyName = "EndDate";
// A helper definition that will not be saved to SQLite directly.
// This property reads and writes to the _raw property.
[Ignore]
public DateTime EndDate {
// Watch out for time zones, as they are not encoded into
// the database. Here, I make no assumptions about time
// zones.
get { return EndDate_raw != null ? DateTime.Parse(EndDate_raw) : EndDate = DateTime.Now; }
set { SetProperty(EndDate_raw, EndDate_ConvertToString(value), (val) => { EndDate_raw = val; }, EndDate_PropertyName); }
}
// This static method is helpful when you need to query
// on the raw value.
public static string EndDate_ConvertToString(DateTime date)
{
return date.ToString("yyyy-MM-dd");
}
}
尝试这个:
string dateToday = DateTime.Today;
var validDates = Database.Connection.Table<Dates>().Where(x => x.StartDate <= dateToday && x.EndDate >= dateToday).ToList();
因此,问题在于我的班级中的StartDate
和EndDate
属性没有这些列。 它们只是真正的StartDate_Raw
和EndDate_Raw
属性的包装。 哪些是strings
。
因为Raw
列是字符串,这意味着我也无法使用LINQ to SQL使用字符串日期来过滤数据库,所以我很困惑。
我的日期存储为字符串的限制是我正在使用的某些第三方软件Zumero的限制 。 如果我没有使用过它,那我只需将表模式更改为int即可。
所以我最后要做的是将SQL数据库中的所有DateTime
列更改为Ticks并存储为Ints
这意味着我可以将string
DateTime属性更改为int
并使用LINQ to SQL,如下所示:
var validDates = Database.Connection.Table<Dates>()
.Where(x => x.StartDate_Raw <= DateTime.Today && x.EndDate_Raw >= DateTime.Today)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.