【发布时间】: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