Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 获取错误:日期比较时System.NotSupportedException_Sqlite_Xamarin_Xamarin.android_Xamarin.forms_Android Sqlite - Fatal编程技术网

Sqlite 获取错误:日期比较时System.NotSupportedException

Sqlite 获取错误:日期比较时System.NotSupportedException,sqlite,xamarin,xamarin.android,xamarin.forms,android-sqlite,Sqlite,Xamarin,Xamarin.android,Xamarin.forms,Android Sqlite,因此,我尝试在我的UserDatabase.cs类中使用此方法检索特定日期的类“Sugar”的实例: public List<Sugar> GetSugarDate(DateTime dt) { var bld = database.Table<Sugar>().Where(mi => mi.Time.Date == dt.Date).ToList(); return bld; } 现在,错误如下所示: public class

因此,我尝试在我的UserDatabase.cs类中使用此方法检索特定日期的类“Sugar”的实例:

public List<Sugar> GetSugarDate(DateTime dt)  
{  
    var bld = database.Table<Sugar>().Where(mi => mi.Time.Date == dt.Date).ToList();  
    return bld;  
}  
现在,错误如下所示:

public class Sugar
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public DateTime Time { get; set; }
    public int DrinkCount { get; set; }

    public Sugar()
    {
        Time = DateTime.Now;
        DrinkCount = 0;
    }
}  
System.NotSupportedException:成员访问未能在SQLite处编译表达式。TableQuery'1[T]。CompileExpr(System.Linq.Expressions.expression,System.Collections.Generic.List'1[T]queryArgs)[0x006cc]in:0在SQLite.TableQuery'1[T]。CompileExpr(System.Linq.Expressions.expression,System.Collections.Generic.List'1[T]queryArgs)[0x0009d]in:0位于SQLite.TableQuery'1[T]。CompileExpr(System.Linq.Expressions.Expression,System.Collections.Generic.List'1[T]queryArgs)[0x0005f]in:0位于SQLite.TableQuery'1[T]。CompileExpr(System.Linq.Expressions.Expression,System.Collections.Generic.List'1[T]queryArgs)[0x00008]in:0位于 /Users/builder/jenkins/workspace/xamarin-android/external/mono/mcs/class/referencesource/Generic/List.cs:98中的System.Collections.Generic.List'1[T]…ctor(System.Collections.Generic.IEnumerable'1[T]集合)[0x00062]

错误发生在以下特定行:

var bld = database.Table<Sugar>().Where(mi => mi.Time.Date == dt.Date).ToList();
var bld=database.Table().Where(mi=>mi.Time.Date==dt.Date).ToList();

我哪里出错了?

表达式的这一部分无法转换为SQL

mi => mi.Time.Date
您可以尝试使用:

var bld = database.Table<Sugar>().ToList().Where(mi => mi.Time.Date == dt.Date).ToList();
var bld=database.Table().ToList().Where(mi=>mi.Time.Date==dt.Date).ToList();
无法使用Linq将访问
DateTime
列(
mi.Time.Date
)的日期部分转换为SQL,因此,您的语句最初失败。因此,使用
ToList
获取内存中的所有记录,允许我们使用Linq to Object方法notLinq to SQLite进行查询


这有点僵硬,但我要做的是:

public List GetSugarDate(DateTime dt)
{
var dayStart=dt.Date.AddDays(-1);
var dayEnd=dt.Date.AddDays(1);
var bld=database.Table().Where(mi=>mi.Time>dayStart&&mi.Time
另外,请确保使用以下命令初始化数据库连接:

SQLiteConnection数据库=新的SQLiteConnection(\u路径,false);

如果您正在查询可为空的日期时间

我猜您的错误是在
mi.Time.Date
。如果数据库中没有“mi”,它将引发空引用异常。请尝试这样编码:
mi?.Time.Date
。这样做会产生一个内联错误:表达式树lambda可能不包含空传播运算符。代码正在编译这不是一个好的解决方案,因为.ToList将在内存中加载整个表记录!但这就是它工作的原因:它首先加载内存中的所有数据,然后查询内存中的数据,而不是尝试将谓词转换为sql语句(这会触发异常,因为它无法将谓词转换为sql语句)。
var bld = database.Table<Sugar>().ToList().Where(mi => mi.Time.Date == dt.Date).ToList();