mgernand / LiteDB.Queryable

An IQueryable wrapper implementation for LiteDB with additional async extensions.
MIT License
22 stars 2 forks source link

OrderBy called once but applied multiple times #22

Closed ismailbennani closed 8 months ago

ismailbennani commented 8 months ago

Hello,

I get the following exception when I use the same ordered query multiple times. It seems like the query builder tries to apply the ordering twice, once per call to Count instead of just once. Is it a bug or am I missing something ?

Thank you for your time!

Reproduction:

LiteDatabase db = new("test.db");
ILiteCollection<TestObject> col = db.GetCollection<TestObject>();

IQueryable<TestObject> query = col.AsQueryable();
IOrderedQueryable<TestObject> orderedQuery = query.OrderBy(o => o.Field);

Console.WriteLine(orderedQuery.Count());
Console.WriteLine(orderedQuery.Count());

class TestObject
{
    public string Field { get; set; }
}
0
Unhandled exception. System.ArgumentException: ORDER BY already defined in this query builder             
   at LiteDB.LiteQueryable`1.OrderBy(BsonExpression keySelector, Int32 order)                             
   at LiteDB.Queryable.LiteQueryBuilder`1.ApplyOrderBy(Expression expression, Boolean isAsync)            
   at LiteDB.Queryable.LiteQueryBuilder`1.Execute[TResult](Expression expression, Boolean isAsync)        
   at LiteDB.Queryable.LiteQueryBuilder`1.Execute[TResult](Expression expression)                         
   at LiteDB.Queryable.LiteQueryProvider`1.Execute[TResult](Expression expression)                        
   at Program.<Main>$(String[] args)
mgernand commented 8 months ago

Hi @ismailbennani, I'll have a lot at it.

mgernand commented 8 months ago

Hi @ismailbennani,

you were right, the Count (and all other operators) are applied a second time for the second call to Count(). The LiteDB query then throws the ArgumentException.

I fixed this and added reproduction tests for this issue. The new release v2.2.1 will contain the fix and will be out any minute.

Thanks for reporting this. 🙂

Cheers, Matt

mgernand commented 8 months ago

Fixed in v2.2.1