2

I try to find entity by its id in liteDb. But the wrong entity returns as a result (with another id). It's reproduced only on one entity for one client (other clients works good).

I use LiteDB 4.1.2.0 in my program, also I tried to find the entity in LiteDBViewer (4.1.0.0).

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

//dbOrders is LiteCollection<Order>

I expect that liteDb will return entity with my id (9fe943d3-97d4-4301-8279-eca89b4209ee), but the actual output entity with id = 2aba5886-ca30-4d67-9cf8-558441ef5eb6.

The result of liteDbViewer: https://i.ibb.co/WntgmZK/2019-08-16-1230.png

1 Answers1

1

Welcome to the community!

Based on the information you provided, you could try the following:

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

Make sure that the attribute "Id" in column "Orders" is of type "guid". If it isn't, either make it one, or use id.ToString() if it's of string type.

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

EDIT:

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!");
    }
}

I'm not sure where the problem could be. The above code works as expected. The only difference is that you're using LiteCollection<Order> instead of GetCollection<Order>. Could you share its structure in your main post?

SpiritBob
  • 2,355
  • 3
  • 24
  • 62