c# - SQLite.net 表查询抛出 NullReferenceException

标签 c# sqlite async-await sqlite-net

我正在使用以下代码行从 SQLite 数据库中的给定表中查询一些数据(平台是 WP81,但我想这在这里无关紧要)。

return await Connection.Table<WorkDay>().Where(wd => wd.Date >= @from && wd.Date <= to).ToListAsync().ConfigureAwait(false);

当我执行我的代码时,我在 Where 子句中得到一个 NullReferenceException。当我删除 where 条件时,一切正常。

return await Connection.Table<WorkDay>().ToListAsync().ConfigureAwait(false);

为了确保我的表中的所有条目都有效并且日期列中没有空值,我使用了一个 SQL 工具来查看 SQLite 数据库。

由于我无法调试 lambda 表达式,所以我对如何在此处找到问题感到困惑。我的假设是由于异步处理而出现问题。

编辑: 这是异常的确切堆栈跟踪

{System.NullReferenceException: Object reference not set to an instance of an object.
   at SQLite.Net.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs)
   at SQLite.Net.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs)
   at SQLite.Net.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs)
   at SQLite.Net.TableQuery`1.GenerateCommand(String selectionList)
   at SQLite.Net.TableQuery`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at SQLite.Net.Async.AsyncTableQuery`1.<ToListAsync>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at TimeStamp.Core.Services.DataService.<GetWorkDays>d__c.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at TimeStamp.Core.ViewModel.MainViewModel.<LoadWorkDays>d__1a.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)}

编辑 2:

我又玩了一会儿,发现了以下内容。

var query = Connection.Table<WorkDay>().Where(wd => wd.Date >= @from && wd.Date <= to);
return query.ToListAsync().ConfigureAwait(false);

执行此语句时,它实际上是在 ToListAsync() 方法而不是 Where 方法中中断。但是,这也无济于事。

后来我尝试了以下确实有效。

var result = await Connection.Table<WorkDay>().ToListAsync().ConfigureAwait(false);
return result.Where(wd => wd.Date >= @from && wd.Date <= to).ToList();

所以我所做的实际上是将 Where 方法分开。但是,尽管这对我有用,但它没有回答我的问题,因为我仍然想知道为什么这不起作用。

最佳答案

我很确定你已经不在这个项目中工作了,但我会回答这个问题,因为它可能会帮助那些仍在为这个问题而苦苦挣扎的人。

问题是你的实体,我不知道到底是什么问题,因为你没有在这里发布你的类(class)。但基本上您可能正在尝试访问尚未从数据库中获取的属性。例如,我有一个具有两个属性的类:

public class MyClass
{
      public DateTime Date { get; set; } // You're saving this property in the database
      public bool IsLate => Date < DateTime.Today; // This property is not be saving, and probably is the cause of your exception
}

我不知道 SQLite 为什么这样做,但是当它查询您的实体时,如果您在查询中按 IsLate 属性进行过滤,它会崩溃,因为 Date 尚未获取属性。

要解决此问题,您必须先获取实体,然后按此属性进行过滤。这基本上就是您在 EDIT 2 中所做的。您已获取所有实体然后进行过滤。

关于c# - SQLite.net 表查询抛出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26131341/

相关文章:

c# - 如何跟踪asp :button click with Google Tag Manager

C# - 带矩阵的文本文件 - 划分所有条目

SQL TABLE 命名。人与人

iphone - 简单的(SQLite-)数据库框架

c# - 处理所有已注册的服务?

c# - ASP.Net MVC Core 2 - 区域路由

android - 从 Android 中的数据库中检索日期明智的数据

javascript - 异步函数按顺序运行,为什么?

c# - 自定义 Microsoft.AspNet.Identity.EntityFramework.UserStore<TUser>

Dart:如何为 Future.wait 清理 "await"?