I'm using the following line of code to query some data from a given table in a SQLite database (platform is WP81 but I guess this doesn't matter here).
return await Connection.Table<WorkDay>().Where(wd => wd.Date >= @from && wd.Date <= to).ToListAsync().ConfigureAwait(false);
When I execute my code I get a NullReferenceException in the Where clause. When I remove the where condition everything works fine.
return await Connection.Table<WorkDay>().ToListAsync().ConfigureAwait(false);
In order to make sure that all entries in my table are valid and there is no null value in the Date column I used an SQL tool to look into the SQLite database.
As I can't debug lambda expressions I'm kind of stuck on how to find the issue here. My assumption is that something goes wrong due to the async handling.
Edit: Here is the exact stacktrace of the exception
{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)}
Edit 2:
I played around a bit more and figured out the following.
var query = Connection.Table<WorkDay>().Where(wd => wd.Date >= @from && wd.Date <= to);
return query.ToListAsync().ConfigureAwait(false);
When executing this statement it actually breaks in the ToListAsync() method instead of the Where method. However, this doesn't help either.
Later I tried the following which actually works.
var result = await Connection.Table<WorkDay>().ToListAsync().ConfigureAwait(false);
return result.Where(wd => wd.Date >= @from && wd.Date <= to).ToList();
So what I did is to separate the Where method actually. But although this works for me it does not answer my question because I'm still wondering why this does not work.
from
andto
variables defined? I also seeto
here does not have an@
. and do you have nullDate
s inWorkDay
? – CaudexCompileExpr
and see what exactly is breaking. – Schist