c# - LiteDb 集合在按 id 搜索时返回无效数据

标签 c# litedb

我尝试通过 liteDb 中的 ID 查找实体。但是结果返回了错误的实体(带有另一个 id)。 它仅在一个实体上为一个客户复制(其他客户工作良好)。

我在我的程序中使用 LiteDB 4.1.2.0,我也试图在 LiteDBViewer (4.1.0.0) 中找到实体。

var id = Guid.Parse("9fe943d3-97d4-4301-8279-eca89b4209ee"); 
var order = dbOrders.FindById(id);

//dbOrders is LiteCollection<Order>

我预计 liteDb 将返回带有我的 ID (9fe943d3-97d4-4301-8279-eca89b4209ee) 的实体,但实际输出的实体带有 id = 2aba5886-ca30-4d67-9cf8-558441ef5eb6。

liteDbViewer 的结果:https://i.ibb.co/WntgmZK/2019-08-16-1230.png

最佳答案

欢迎来到社区!

根据您提供的信息,您可以尝试以下操作:

var id = Guid.Parse("9fe943d3-97d4-4301-8279-eca89b4209ee"); 
var order = dbOrders.FindOne(o => o.Id == id);

确保“Orders”列中的属性“Id”的类型为“guid”。如果不是,要么做一个,要么使用 id.ToString()如果是字符串类型。

https://github.com/mbdavid/LiteDB/wiki/Queries

编辑:

static void Main(string[] args)
{
    // Open database (or create if not exits)
    using (var db = new LiteDatabase(@"MyData.db"))
    {
        // Get user collection
        var users = db.GetCollection<User>("users");

            var id = Guid.Parse("8dd0997e-42b1-432d-820e-4637dd08fa2e");
            //var userById = users.FindOne(x => x.Id == id);
            var userById = users.FindById(id);
            if (id != userById.Id)
               Console.WriteLine("Error!");
    }
}

我不确定问题出在哪里。上面的代码按预期工作。唯一的区别是您使用的是 LiteCollection<Order>而不是 GetCollection<Order> .你能在你的主帖中分享它的结构吗?

关于c# - LiteDb 集合在按 id 搜索时返回无效数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57523415/

相关文章:

mysql - 包含用户数据和中央数据库更新的数据库设计

c# - 使用top子句查询数据表

c# - 强制 Encoding.UTF8.GetString 抛出 ArgumentException

c# - 在 C# 中从 Windows Media Player 上播放的视频中获取图像

c# - 具有多个数据库的存储库模式

c# - 如何在不检查 null 或 0 的情况下为 sqlparameter NULL 值赋值?

c# - 获取错误 System.IO.IOException : 'The process cannot access the file'