Search code examples
c#sqlitemonosqlite.net

How do I use Sqlite.Net Extensions Filter


I am using the Sqlite.Net Extensions library to get objects from my database.

I have a list of Id's and I would like to get all the objects from the database where my list contains that Id.

I have got the following:

var filteredCalls = listOfIds;            
var conn = Databsae.Connection;
var NewCalls = conn.GetAllWithChildren<Call>(x => filteredCalls.Any(y => y == x.Id));

But this code returns the error:

[ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NotSupportedException: Cannot compile: Lambda 08-30 15:46:57.210 E/mono-rt (16849): at SQLite.Net.TableQuery1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List1 queryArgs) [0x007aa] in :0 08-30 15:46:57.210 E/mono-rt (16849): at SQLite.Net.TableQuery1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List1 queryArgs) [0x001a5] in :0 08-30 15:46:57.210 E/mono-rt (16849): at SQLite.Net.TableQuery1[T].GenerateCommand (System.String selectionList) [0x0006d] in <filename unknown>:0 08-30 15:46:57.210 E/mono-rt (16849): at SQLite.Net.TableQuery1[T].GetEnumerator () [0x00008] in :0 08-30 15:46:57.210 E/mono-rt (16849): at System.Collections.Generic.List1[T]..ctor (IEnumerable1 collection) [0x00073] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/mscorlib/system/collections/generic/list.cs:98 08-30 15:46:57.210 E/mono-rt (16849): at System.Linq.Enumerable.ToList[TSource] (IEnumerable1 source) [0x00011] in /Users/builder/data/lanes/3540/1cf254db/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:835 08-30 15:46:57.210 E/mono-rt (16849): at SQLiteNetExtensions.Extensions.ReadOperations.GetAllWithChildren[T] (SQLite.Net.SQLiteConnection conn, System.Linq.Expressions.Expression1 filter, Boolean recursive) [0x00015] in /Users/redent/Documents/workspace/sqlite-net-extensions/SQLiteNetExtensions/Extensions/ReadOperations.cs:60

So how am I supposed to use the GetAllWithChildren method without it crashing? The documentation is quite sparse

Extra

This is the line where the method is implemented

Possibly lambda expressions are not supported in Sqlite as stated here if this is the case, what is the alternative / how was this method supposed to be used?


Solution

  • So it looked like Sqlite.Net could not cope with the Lambda expression nested within another lamdba expression. I therefore changed my filter to be:

    var NewCalls = conn.GetAllWithChildren<DiaryCall>(x => filteredCalls.Contains(x.Id));