System.NotSupportedException:无法编译:TypeAs在SQLite.TableQuery

时间:2016-01-10 22:24:41

标签: c# windows sqlite

我正在使用C#和SQLite编写一个Windows应用商店应用。在这种情况下,我遇到了一个问题,我搜索了解决方案。但我没有运气。问题是 我想从表中删除一条记录。我的桌子就像这样

 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; }
    }`

我的删除方法如下:

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();
            }
        }

我发现下面显示的错误:

    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()

1 个答案:

答案 0 :(得分:1)

错误在这一行:

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

这里的问题是你传递给Where方法的lambda表达式是SQLite LINQ驱动程序无法弄清楚如何处理的。

为了解决这个问题,您需要获取外部值并将其作为简单的字符串变量引用传递,如下所示:

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

结果lambda表达式非常简单,SQLite可以从。

生成SQL表达式