WhoAmI7 WhoAmI7
WhoAmI7 WhoAmI7

Reputation: 33

System.NotSupportedException: Cannot compile: TypeAs At SQLite.TableQuery

I am writing a windows store app using C# and SQLite. In this case I am facing a problem and I searched for solution for that. But I got no luck. The problem is I want to delete a record from table. My table is like this

 class DocumentRecord
    {
         [PrimaryKey, AutoIncrement]
         public int dID { get; set; }
         public string dName { get; set; }
         public string dDescription { get; set; }
         public byte[] dImage { get; set; }
         public int uID { get; set; }
         public string dTextData { get; set; }
         public DateTime dCreatedDate { get; set; }
         public DateTime dUpdatedDate { get; set; }
    }`

My deletion method is as follows:

private async void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            //confirmation();
            SQLiteAsyncConnection dbconn = new SQLiteAsyncConnection("Data.db");
            var DeleteItem = await dbconn.Table<DocumentRecord>().Where(x => x.dName == (App.Current as App).documentName).FirstOrDefaultAsync();
            if (DeleteItem != null)
            {
                await dbconn.DeleteAsync(DeleteItem);
                var dlge = new MessageDialog("You successfully deleted a document !");
                await dlge.ShowAsync();
            }
        }

I am catching the error shown below:

    System.NotSupportedException: Cannot compile: TypeAs at SQLite.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs)
at SQLite.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs)
at SQLite.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs)
at SQLite.TableQuery`1.GenerateCommand(String selectionList)
at SQLite.TableQuery`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at SQLite.TableQuery`1.FirstOrDefault()
at SQLite.AsyncTableQuery`1.<FirstOrDefaultAsync>b__6()
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.TaskAwaiter`1.GetResult()
at MMUD.LoadFlyout.<btnDelete_Click>d__6.MoveNext()

Upvotes: 3

Views: 1312

Answers (1)

Corey
Corey

Reputation: 16574

The error is on this line:

var DeleteItem = await dbconn.Table<DocumentRecord>().Where(x => x.dName == (App.Current as App).documentName).FirstOrDefaultAsync();

The problem here is that the lambda expression you're passing to the Where method is something that the SQLite LINQ driver can't figure out how to deal with.

In order to fix this you need to get the value outside and pass it in as a simple string variable reference like this:

string documentName = (App.Current as App).documentName;
var DeleteItem = await dbconn.Table<DocumentRecord>().Where(x => x.dName == documentName).FirstOrDefaultAsync();

The resultant lambda expression is simple enough for SQLite to generate an SQL expression from.

Upvotes: 1

Related Questions