2

我正在编写一个移动应用程序,它使用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.List1[T] queryArgs) [0x00613] in <8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W/Xamarin。 Insights(31238):在 SQLite.Net.TableQuery 1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List1[T] queryArgs) [0x0064b] in <8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W/Xamarin.Insights(31238):在 SQLite.Net.TableQuery 1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List1 [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.List1[T] queryArgs) [0x00027] in <8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W/Xamarin.Insights(31238): 在 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.TableQuery1[T].GetEnumerator () [0x00008] <8f2bb39aeff94a30a8628064be9c7efe>:0 09-28 13:01:24.293 W/Xamarin.Insights(31238):在 System.Collections.Generic.List 1[T]..ctor (System.Collections.Generic.IEnumerable1[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

我对StartDateand的定义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");     
    }
}
4

2 回答 2

1

尝试这个:

string dateToday = DateTime.Today;
var validDates = Database.Connection.Table<Dates>().Where(x => x.StartDate <= dateToday && x.EndDate >= dateToday).ToList();
于 2016-09-28T13:15:13.980 回答
0

所以问题是我StartDateEndDate我的类中的属性不是列。它们只是真实StartDate_RawEndDate_Raw属性的包装器。哪些是strings.

因为这些Raw列是字符串,这意味着我也无法使用LINQ to SQL使用字符串日期过滤数据库,所以我很困惑。

我的日期存储为字符串的限制是我使用的一些名为Zumero的第三方软件的限制。如果我没有使用它,我只会将我的表模式更改为 int 。

所以我最终做的是将DateTime我的 SQL 数据库中的所有列更改为Ticks并存储,因为Ints这意味着我可以将stringDateTime 属性更改为int并使用 LINQ to SQL,如下所示:

var validDates = Database.Connection.Table<Dates>()
        .Where(x => x.StartDate_Raw <= DateTime.Today && x.EndDate_Raw >= DateTime.Today)
       .ToList();
于 2016-10-31T16:11:13.480 回答