winforms 如何在C#、LiteDB中按值删除记录?

roqulrg3  于 2022-11-25  发布在  C#
关注(0)|答案(1)|浏览(364)
private void DeleteIncomeTransactionBtn_Click(object sender, EventArgs e)
    {
        var incomeTransactions = DbContext.GetInstance().GetCollection<IncomeTransaction>("income_transactions")
                   .FindAll();

        var code = inputTransactions.SelectedText;
        var ensure = DbContext.GetInstance().GetCollection<IncomeTransaction>("income_transactions").EnsureIndex(r => r.TransactionCode);

        var query = DbContext.GetInstance().GetCollection<IncomeTransaction>("income_transactions").Query()
            .Where(r => r.TransactionCode == code)
            .Select(r => r.Id);

        DbContext.GetInstance().GetCollection<IncomeTransaction>("income_transactions").Delete(query));

    }

我想按下拉列表的选定索引(TransactionCode)删除一条记录。在此代码中,我首先尝试按选定索引查找记录,然后使用此查找此记录的ID。然后删除具有此ID的记录。请帮助我解决此错误。我的错误:无法从“LiteDB.ILiteQueryableResult”转换为“LiteDB.BSON值”。

6ss1mwsb

6ss1mwsb1#

您应该使用DeleteMany方法:

var deletedCount = DbContext.GetInstance()
.GetCollection<IncomeTransaction>("income_transactions")
.DeleteMany(r => r.TransactionCode == code);

这将返回删除的记录数。

相关问题