mgernand/LiteDB.Queryable

OrderBy called once but applied multiple times

ismailbennani opened this issue · 3 comments

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)

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

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

Fixed in v2.2.1