【问题标题】:Getting Error: System.NotSupportedException when Date Comparison出现错误:日期比较时出现 System.NotSupportedException
【发布时间】:2018-01-07 11:01:46
【问题描述】:

所以我试图在我的 UserDatabse.cs 类中使用此方法检索特定日期的“糖”类的实例:

public List<Sugar> GetSugarDate(DateTime dt)  
{  
    var bld = database.Table<Sugar>().Where(mi => mi.Time.Date == dt.Date).ToList();  
    return bld;  
}  

*请记住,该应用目前没有任何 Sugar 实例,因此日期比较介于空日期和实际日期之间。我认为这是导致错误的原因,任何解决方案都将不胜感激。

在另一个类中调用这个方法是这样的:

DateTime Time = DateTime.Now;

ObservableCollection<Image> Sugar_Count = new ObservableCollection<Image>();
Image s = new Image();
s.Source = "drawable/soda.png";
var xa = App.Database.GetSugarDate(Time);

Sugar.cs 类定义如下:

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 expr, System.Collections.Generic.List'1[T]queryArgs)[0x006cc] 处编译表达式]in :0 在 SQLite.TableQuery'1[T].CompileExpr(System.Linq.Expressions.Expression expr, System.Collections.Generic.List'1[T]queryArgs)[0x0009d]in : 0 在 SQLite.TableQuery'1[T].CompileExpr(System.Linq.Expressions.Expression expr, System.Collections.Generic.List'1[T]queryArgs)[0x0005f]in :0 在 SQLite.TableQuery' 1[T].CompileExpr(System.Linq.Expressions.Expression expr, System.Collections.Generic.List'1[T]queryArgs)[0x00008]in :0 at System.Collections.Generic.List'1[T]..ctor(System.Collections.Generic.IEnumerable'1[T]collection)[0x00062] 在 /Users/builder/jenkins/workspace/xamarin-android/external/mono /mcs/class/referencesource/generic/list.cs:98 .....

错误发生在这一行:

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

我哪里错了?

【问题讨论】:

  • 我猜你的错误是mi.Time.Date。如果您的数据库中没有“mi”,它将引发空引用异常。尝试这样编码:mi?.Time.Date.
  • 这样做会产生内联错误:表达式树 lambda 可能不包含空传播运算符

标签: sqlite xamarin xamarin.android xamarin.forms android-sqlite


【解决方案1】:

这部分表达式无法翻译成 SQL。

mi => mi.Time.Date

您可以尝试使用:

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

无法使用 Linq 将访问 DateTime 列 (mi.Time.Date) 的日期部分转换为 SQL,因此,您的语句最初失败。因此,使用ToList 获取内存中的所有记录,允许我们使用 Linq to Object 方法而不是 Linq to SQLite 进行查询。

【讨论】:

  • 代码正在编译你的答案,但它与我已有的有什么不同?
  • 这不是一个好的解决方案,因为 .ToList 会将整个表记录加载到内存中!但这就是它起作用的原因:它首先将所有数据加载到内存中,然后查询内存中的数据,而不是尝试将谓词转换为 sql 语句(这会触发异常,因为它无法将谓词转换为 sql 语句)。
【解决方案2】:

它有点笨拙,但我已经解决了这个问题:

public List<Sugar> GetSugarDate(DateTime dt)
{
    var dayStart = dt.Date.AddDays(-1);
    var dayEnd = dt.Date.AddDays(1);

    var bld = database.Table<Sugar>().Where(mi => mi.Time > dayStart && mi.Time < dayEnd).ToList();
    return bld;
}

同时确保你初始化你的数据库连接:

SQLiteConnection database = new SQLiteConnection(_path,false);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2016-02-17
    • 2014-10-23
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多