mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.52k stars 1.24k forks source link

Does this work now? #2256

Closed dangergrrl closed 1 year ago

dangergrrl commented 1 year ago
    Thanks guys! It's really nice to know about this. I'm still working v5 version that will the a biggest change in this project. In this case, you will resolve this using a single SQL query (or Linq query) like:

SELECT MAX(_id) FROM your_col or col.Query().Max(x => x.Id)

Originally posted by @mbdavid in https://github.com/mbdavid/LiteDB/issues/1128#issuecomment-442554881

ProKn1fe commented 1 year ago

I think it works.

        [Fact]
        public void TestLinqMax()
        {
#if NET5_0_OR_GREATER
            var random = Random.Shared;
#else
            var random = new Random();
#endif

            using var ms = new MemoryStream();
            using var litedb = new LiteDatabase(ms);

            var list = new List<TestModel>();
            for (var a = 0; a < 100; ++a)
            {
                list.Add(new TestModel() { Count = random.Next() });
            }
            var max = list.Max(a => a.Count);

            var collection = litedb.GetCollection<TestModel>();
            collection.InsertBulk(list);

            var maxFromCollection = collection.Max(a => a.Count);
            Assert.Equal(max, maxFromCollection);
        }

        private class TestModel
        {
            public int Count { get; set; }
        }
    }
dangergrrl commented 1 year ago

Sorry, I didn't want to actually try because my project is rather large and not debugged just now (lots of new code). Ty, mbdavid for all the wonderful code.