gpt4 book ai didi

c# - System.NotSupportedException : Cannot compile: TypeAs At SQLite. 表查询

转载 作者:可可西里 更新时间:2023-11-01 09:33:12 25 4
gpt4 key购买 nike

我正在使用 C# 和 SQLite 编写 Windows 商店应用程序。在这种情况下,我遇到了一个问题,我为此寻找了解决方案。但我没有运气。问题是我想从表中删除一条记录。我的 table 是这样的

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

最佳答案

错误在这一行:

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 表达式。

关于c# - System.NotSupportedException : Cannot compile: TypeAs At SQLite. 表查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34711506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com