【问题标题】:System.NotSupportedException: Cannot compile: TypeAs At SQLite.TableQuerySystem.NotSupportedException:无法编译:TypeAs 在 SQLite.TableQuery
【发布时间】:2016-01-20 21:11:43
【问题描述】:

我正在使用 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()

【问题讨论】:

  • 如果您将文本添加为​​文本而不是图像,则效果会更好,更有用。搜索不会读取图像中的文本。请您努力在您的帖子中的代码块中添加错误文本。感谢您的理解与合作。

标签: c# windows sqlite


【解决方案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 表达式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 2015-06-07
    • 2013-03-06
    相关资源
    最近更新 更多