我正在编写一个移动应用程序,它使用Sqlite.Net
我试图过滤我的Table
使用DateTime
属性和LINQ to SQL
我有以下代码:
var validDates = Database.Connection.Table<Dates>()
.Where(x => x.StartDate <= DateTime.Today && x.EndDate >= DateTime.Today)
.ToList();
但这会引发未设置为对象实例的对象引用并且堆栈跟踪似乎表明错误在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] in <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
1[T] queryArgs) [0x0064b] in <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
1 [T] queryArgs) [0x00027] in <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
1[T] queryArgs) [0x00027] in <8f2bb39aeff94a30a8628064be9c7efe>: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].GetEnumerator () [0x00008] <8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W/Xamarin.Insights(31238):在 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] source) [0x00011] 在 /Users/builder/data/lanes/3819/c1d1c79c/source/mono/mcs/class/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);
但是有没有办法DateTime
使用LINQ to SQL进行查询?
额外信息
我StoreDateTimeAsTicks
的设置设置为True
我对StartDate
and的定义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");
}
}